Search code examples
androidandroid-recyclerviewd-pad

Recyclerview and D-pad navigation


I'm developing an app that needs to use D-pad navigation in a RecyclerView. My problem is that although I set android:focusable=true in the items.xml(news_items), it doesn't seems to work.

My question is: how should I implement D-pad navigation in a RecyclerView?

The RecyclerView is inside the following layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

         />
    <TextView
        android:id="@+id/emptyView"
        android:visibility="gone"
        tools:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=":( \n No news available"
        android:textSize="@dimen/fsn_emty_text"
        android:layout_gravity="center_vertical"
        android:gravity="center_horizontal"
        android:paddingEnd="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingStart="@dimen/activity_horizontal_margin"
        />

</FrameLayout>

news_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/news_ripple"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"

    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.15"
                android:background="?attr/colorAccent"

                />


            <LinearLayout

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/ni_margin"
                android:layout_weight="9"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvTitulo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:gravity="left"
                    android:textColor="?android:attr/textColorPrimary"
                    tools:text="Titulo" />

                <TextView
                    android:id="@+id/tvFecha"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    tools:text="Fecha" />

                <TextView
                    android:id="@+id/tvPublisher"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    tools:text="Publisher" />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

UPDATE: This is the focus order I'am receiving now: The red color is the focus order I'am getting now, when I click one time from the tab to down, the RecyclerView(the entire reycclerview) gets the focus, one more click down and the AdMob(showed in black) get the focus. The blue arrow is what I want inside the RecyclerView

enter image description here


Solution

  • Adding android:descendantFocusability="afterDescendants" solved my problem

    <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="afterDescendants"
                 />