【问题标题】:Android Tv RecyclerView set next focus of its itemAndroid Tv RecyclerView 设置其项目的下一个焦点
【发布时间】:2018-08-27 22:34:27
【问题描述】:

我正在开发 android 电视,在我们的 Fragment 中有两个 Horizo​​ntal Recyclerview,当我使用 D-pad 朝右方向滚动 First Recyclerview 时,它滚动良好,当我到达其 Recycler 视图的最后一个焦点项目时,会自动聚焦转到第二个 Recycler 视图项。

如何预防?

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

【问题讨论】:

    标签: java android


    【解决方案1】:

    可能为时已晚,但对于仍在寻找解决方案的任何人:

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

    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)
        }
    }
    

    并且此方法将成为您的布局管理器的一部分。

    【讨论】:

      【解决方案2】:

      尝试将recyclerviewfocusable属性设置为false

      recyclerview.setFocusable(false);
      

      【讨论】:

        【解决方案3】:

        我很晚才发布这个答案,但有一种方法可以做到,在 Adapter 类中检查项目是否是第一个,并将 nextFocusLeftId 设置为自身。 例如。 itemview.setnextFocusLeftId(itemview.gedId) 对回收站视图的最后一个元素执行相同的操作。

        【讨论】:

          猜你喜欢
          • 2020-10-11
          • 2017-01-07
          • 2021-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-05
          • 2016-03-20
          • 2019-04-30
          相关资源
          最近更新 更多