How to support dpad controls for RecyclerView
Asked Answered
A

3

11

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

Alodie answered 11/2, 2016 at 3:3 Comment(2)
I have had a similar problem. I don't know how to shift the focus. I am going to try and manually handle all of the dpad input and basically fake it. I will report back.Intumesce
@Vpd, do you think that this I ended up overriding the onKeyListener for RecylcerView to make it work is the right way to do. I too had this doubt in mind, coz a remote can have many key events that we may need to handle wen taking up this approach. And it gets complex as we try to track key events and perform operations. There must be a some kind of google libs for tv apps for tracking and manipulating key events, Any ideas and implementation link will be helpful to me in case you figured out?Septuor
F
5

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

Fpc answered 20/1, 2017 at 11:59 Comment(2)
What do you mean by root view here?Marplot
parent viewgroup (layout)Fpc
D
2

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

Discontinuation answered 18/4, 2016 at 18:29 Comment(0)
I
1

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();
                }
            }
        });
Intumesce answered 13/2, 2016 at 12:4 Comment(2)
Thanks for your reply. I ended up overriding the onKeyListener for RecylcerView to make it work.Alodie
oh also a solid plan!Intumesce

© 2022 - 2024 — McMap. All rights reserved.