Android TV listview及焦点处理

Android TV上的listview ,因为没有touch事件,只能通过按键处理,因此,用到listview时需要特殊处理:

1.复杂的view需要获取焦点,需要设置:

setItemsCanFocus(true)

同时需要设置下能获取焦点view的属性:

android:focusable="true

这样子级view就可以获取获取焦点。

2.view中需要获取焦点需要高亮框效果,可以在view画外框:

package com.cn21.ecloud.tv.ui.widget;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Rect;

import android.graphics.drawable.Drawable;

import android.util.AttributeSet;

import android.widget.RelativeLayout;

public class SelectedRelativeLayout extends RelativeLayout{

private Drawable mFloatDrawable;

private Rect mTempRect = new Rect();

public SelectedRelativeLayout(Context context) {

this(context, null, 0);

}

public SelectedRelativeLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public SelectedRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

mFloatDrawable = getResources().getDrawable(R.drawable.item_float_rectangle);

}

@Override

protected void dispatchDraw(Canvas canvas) {

super.dispatchDraw(canvas);

if (hasFocus()) {

if (mFloatDrawable != null) {

final int w = getMeasuredWidth();

final int h = getMeasuredHeight();

mFloatDrawable.getPadding(mTempRect);

mFloatDrawable.setBounds(-mTempRect.left, -mTempRect.top,

w + mTempRect.right, h + mTempRect.bottom);

mFloatDrawable.draw(canvas);

}

}

}

}

布局中直接使用这个view

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐