如何支持RecyclerView的dpad控件

时间:2016-02-11 03:03:01

标签: android android-recyclerview android-tv d-pad

我目前正在尝试将Android移动应用移植到Android TV。我有一个似乎在我的Android TV应用程序中正确显示的RecyclerView。我正在使用linearLayout作为我的RecyclerView。但我似乎无法使用dpad控件在RecyclerView中导航。

有什么想法吗?

以下是有关的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" />

3 个答案:

答案 0 :(得分:4)

制作项目布局android:focusable="true"

的根视图

答案 1 :(得分:2)

尝试在android:descendantFocusability="afterDescendants"

上设置RecyclerView

答案 2 :(得分:0)

我的代码遇到的问题是将重点放在recyclerview的第一个孩子身上。一旦它在那里似乎很好地处理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();
                }
            }
        });