我目前正在尝试将Android移动应用程序移植到Android TV。我有一个回收器视图,它似乎在我的Android TV应用程序中正确显示。我正在为我的RecyclerView使用linearLayout。但我似乎无法使用 dpad 控件在回收器视图中导航。
有什么想法吗?
以下是相关的 xml:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RecyclerView"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/nav_bg"
android:scrollbars="vertical"
tools:showIn="@layout/activity_content" />
将项目布局的根视图设为android:focusable="true"
尝试在RecyclerView
上设置android:descendantFocusability="afterDescendants"
我的代码遇到的问题是将焦点放在回收器视图的第一个子级上。一旦它在那里,它似乎可以很好地处理dpad控件。
在没有看到更多代码的情况下,这是最好的建议。如果这不起作用,请提供您的活动和完整的 xml。祝你好运!
在您的 xml 中:
<!-- make sure this is an attribute for the layout that would
get focus before the recycler view -->
android:nextFocusDown="@+id/RecyclerView"
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RecyclerView"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/nav_bg"
android:scrollbars="vertical"
tools:showIn="@layout/activity_content"
android:focusable="true"
/>
在您的活动中:
public static final int BANNER = 0;
public static final int RECVIEW1 = 1;
public static final int RECVIEW2 = 2;
private static int currFocus = 0;
//you will need to do something similar to this listener for all focusable things
RecyclerView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i("ONFOCUSCHANGE- reclist", "focus has changed I repeat the focus has changed! current focus = " + currFocus);
if(currFocus != RECVIEW1){
currFocus = RECVIEW1;
RecyclerView.getChildAt(0).requestFocus();
}
}
});