Android TV listview及焦点处理

2019/7/7 20:37:06

本文主要是介绍Android TV listview及焦点处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

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



这篇关于Android TV listview及焦点处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程