如何为RecyclerView支持十字键控制

11

我目前正在尝试将Android手机应用程序移植到Android TV上。我有一个RecyclerView,在我的Android TV应用程序中似乎显示正确。我为RecyclerView使用了linearLayout。但是我似乎无法使用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" />

:这是一个空的段落标签。

我曾经遇到过类似的问题。我不知道如何转移焦点。我将尝试手动处理所有dpad输入,基本上是假的。我会回报结果。 - alex_milhouse
@Vpd,你认为这种方式 我最终覆盖了RecylcerView的onKeyListener使其工作 是正确的吗?我也有这个疑问,因为一个遥控器可能有许多按键事件需要我们处理,当采用这种方法时,它变得复杂。对于跟踪按键事件和执行操作,肯定有一些谷歌库适用于电视应用程序。如果你找到了任何想法和实现链接,那么对我来说将会很有帮助。 - DJphy
3个回答

5

将您的项目布局的根视图设置为 android:focusable="true"


这里的“root view”是什么意思? - sam_k
父视图组(布局) - Gurgen Hakobyan

2
尝试在RecyclerView上设置android:descendantFocusability="afterDescendants"

1
我的代码存在问题,无法将焦点放在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();
                }
            }
        });

感谢您的回复。我最终重写了RecylcerView的onKeyListener以使其正常工作。 - Vpd
哦,还是一个很不错的计划! - alex_milhouse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接