Recyclerview和D-pad导航

时间:2016-04-27 10:45:17

标签: android android-recyclerview d-pad

我正在开发一款需要在RecyclerView中使用D-pad导航的应用。我的问题是虽然我在android:focusable=true(news_items)中设置了items.xml,但它似乎无法正常工作。

我的问题是:我应该如何在RecyclerView中实现D-pad导航?

RecyclerView位于以下布局中:

<?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>

更新:这是我现在收到的重点订单: 红色是我现在获得的焦点顺序,当我从标签向下单击一次时,RecyclerView(整个reycclerview)获得焦点,再一次点击,AdMob(显示)黑色)获得焦点。蓝色箭头是我想要的RecyclerView

enter image description here

1 个答案:

答案 0 :(得分:7)

添加android:descendantFocusability =“afterDescendants”解决了我的问题

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