Android TV RecyclerView sets the next focus of its project… here is a solution to the problem.
Android TV RecyclerView sets the next focus of its project
I
work on android TV, I have two Horizontal Recyclerviews in our fragment, it scrolls well when I scroll the first Recyclerview to the right using D-pad, and when I reach the last focus item of its Recycler View, the autofocus drops to the second Recycler View item.
How can this be prevented?
<?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>
Solution
It may be late, but for anyone still looking for a solution:
You should write your own logic when searching for the next focus, if the last View is provided as the next focus item at the end of the list, or you can modify the logic as needed. For example:
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)
}
}
And this method becomes part of the layout manager.