【问题标题】:Recyclerview and D-pad navigationRecyclerview 和方向键导航
【发布时间】:2016-08-21 14:50:30
【问题描述】:

我正在开发一个需要在RecyclerView 中使用方向键导航的应用。我的问题是,虽然我在items.xml(news_items) 中设置了android:focusable=true,但它似乎不起作用。

我的问题是:我应该如何在RecyclerView 中实现方向键导航?

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 中想要的

【问题讨论】:

  • 你的意思是你想关注Recycler View中的新闻项目对吧?
  • 是的,新项目是Recyclerview中每个项目的布局。我希望能够通过 D-pad 遍历 Recyclerview 中的项目
  • @CarlosHernándezGil 你的解决方案是什么?
  • @sam_k 将此添加到回收站视图中,如您在接受的答案中所见:android:descendantFocusability="afterDescendants"

标签: android android-recyclerview d-pad


【解决方案1】:

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

【讨论】:

  • 别忘了制作项目布局的根视图 android:focusable="true"
猜你喜欢
  • 1970-01-01
  • 2011-07-29
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
相关资源
最近更新 更多