Android TV RecyclerView focus interaction

Tepes Lucian picture Tepes Lucian · Aug 31, 2016 · Viewed 12.1k times · Source

I'm currently using a regular RecyclerView with GridLayoutManagerwith different spanCount depeding on the viewType for an Android TV app. All is working decent but I have 2 issues:

  1. If you long press the dpad down to scroll fast between the items sometimes the focus is lost to a view that isn't a child of the RecyclerView.
  2. How can I tell the RecyclerView to keep the current focused view in the center of the grid?

It seems that the issues listed are fixed by using the VerticalGridView from LeanBack library but the LayoutManger that it uses is internal and doesn't support spanCount.

Answer

Sharath kumar picture Sharath kumar · Aug 28, 2017

Recycler view works fine with android TV.Possible solutions you can include are:

1.add focusable and focusableInTouchode to view.Add focusListner through the code and request focus each time when the view is clicked.

2.To keep Recycler View focused item in the centre you have to override layout manager just like this example.

RecyclerView smoothScroll to position in the center. android

or

use layoutManager.scrollToPositionWithOffset(position,offset) where position-focused view position and offset is the recycler view width/2.