Android TV的RecyclerView如何设置其项目的下一个焦点。

3

我正在处理Android TV相关的工作。在我们的Fragment中,有两个水平RecyclerView。当我使用右方向键滚动第一个RecyclerView时,它可以很好地滚动。但是当我到达其RecyclerView的最后一个焦点项时,焦点会自动转移到第二个RecyclerView的项上。

如何防止这种情况发生?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:focusable="false"
android:orientation="vertical">


<RelativeLayout
    android:id="@+id/firstRelativeGrid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:clipToPadding="false"
        android:focusable="false"
        android:paddingLeft="0dp"
        android:paddingRight="755dp" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/gridView2"
        android:layout_width="155dp"
        android:layout_height="200dp"
        android:layout_alignBaseline="@+id/gridView1"
        android:layout_marginLeft="0dp"
        android:focusable="false"
        android:nextFocusRight="@null"
        android:nextFocusUp="@+id/home_textview" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/secondRelativeGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:orientation="vertical">


    <android.support.v17.leanback.widget.MyHorizontalGridView
        android:id="@+id/gridView3"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:clipToPadding="false"
        android:focusable="false"
        android:paddingLeft="0dp"
        android:paddingRight="754dp" />


    <android.support.v17.leanback.widget.MyHorizontalGridView
        android:id="@+id/gridView4"
        android:layout_width="155dp"
        android:layout_height="200dp"
        android:layout_alignBaseline="@+id/gridView3"
        android:layout_marginLeft="0dp"
        android:clipToPadding="false"
        android:descendantFocusability="blocksDescendants"
        android:focusable="false"
        android:paddingLeft="0dp"
        android:paddingRight="-2dp"

        />

</RelativeLayout>

3个回答

5

如果您还在寻找解决方案,可能有点晚了:

在搜索下一个焦点发生时,您应该编写自己的逻辑,并将最后一个视图作为下一个焦点项目提供,如果在列表末尾,则可以修改逻辑以满足您的需求。例如:

override fun onInterceptFocusSearch(focused: View?, direction: Int): View? {
    val position = getPosition(focused)
    val count = itemCount
    lastKnownPosition = position
    return if (position == count - 1 && direction == View.FOCUS_RIGHT) {
        focused
    } else {
        super.onInterceptFocusSearch(focused, direction)
    }
}

这个方法将成为您的布局管理器的一部分。


很好的答案。我有一个相反的问题。我需要将焦点传递到左侧菜单上。当嵌套的RecyclerView在位置0滚动时,焦点正确地移动。当其中一个嵌套的RecyclerView处于不同的位置时,问题就出现了,然后焦点首先向下或向上移动。在将嵌套的RecyclerView滚动到位置0后,焦点移到了左侧菜单。您有什么想法可以强制立即将焦点传递到左侧菜单吗? - kazhiu

0

尝试将 recyclerviewfocusable 属性设置为 false

recyclerview.setFocusable(false);

-1

我很晚才回答这个问题,但是有一种方法可以做到,在适配器类中检查项目是否为第一个,并将nextFocusLeftId设置为自身。 例如。 itemview.setnextFocusLeftId(itemview.gedId) 对于RecyclerView的最后一个元素也要做同样的操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接