How to support dpad controls for RecyclerView

感情迁移 提交于 2019-12-14 00:19:04

问题


I am currently trying to port Android mobile app to Android TV. I have a RecyclerView that seems to be displaying correctly in my Android TV app. I am using linearLayout for my RecyclerView. But I don't seem to be able to navigate inside RecyclerView using dpad controls.

Any ideas?

Here is the concerned 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" />


回答1:


Make root view of your item layout android:focusable="true"




回答2:


Try setting android:descendantFocusability="afterDescendants" on the RecyclerView




回答3:


What my code was having problems doing was giving focus to the first child of the recyclerview. Once it was there it seemed to handle dpad controls well.

Without seeing more of your code, this is the best suggestion. Please provide your activity and full xml if this doesn't work. Good Luck!

In your 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"
  />

In your activity:

    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();
                }
            }
        });


来源:https://stackoverflow.com/questions/35330058/how-to-support-dpad-controls-for-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!