36

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.

Tepes Lucian
  • 908
  • 1
  • 7
  • 17
  • Answering the first question please take a look at a [post](https://stackoverflow.com/a/48309162/4770877) – yoAlex5 Jan 17 '18 at 20:09
  • @Tepes I am using Recycler View and I am not getting Focus will you please tell me how to get focous on recycler view – Nikhil Singh May 31 '19 at 06:25

3 Answers3

4
  1. For a regular RecyclerView,

    • I had to specify: android:descendantFocusability="beforeDescendants"

    • And also android:nextFocusDown="@+id/recyclerviewId" is set to send focus to RV itself.

  2. The only solution I see is a key listener to select item to position currentPosition + spancount.

adboco
  • 2,840
  • 1
  • 21
  • 21
Geoffrey
  • 91
  • 3
3

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.

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • Thanks for the answer. In the end I've used the `VerticalGridView` from the leanback library but seems like your solution should work. – Tepes Lucian Sep 07 '17 at 15:00
  • Your most welcome.VerticalGridView or extending RowFragment can also let you achieve the same effect like the recycler view but the disadvantage is you cannot customize it like the way you want. – Sharath kumar Sep 08 '17 at 04:40
  • I try to follow your guidelines but not working for me. If you please share some code with idea that will be more helpful. – Md Imran Choudhury Oct 24 '22 at 05:21
0

You can try to check this workaround for the bug with RecycleView focus scrolling when navigating with d-pad.

Here is the SO question for that.

The problem here is that the GridLayoutManager uses LinearLayoutManager's implementation of onFocusSearchFailed() which is called when focus approaches the inner border of RecyclerView. LinearLayoutManager's implementation just offers first/last (depends on scrolling direction) element. Hence focus jumps to first/last element of new row.

So, maybe this workaround will solve your issue or give you an idea on how to solve your problem.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Thanks for the response, but I've read about those answers. `v24.2.0` that i'm using jumps to the correct column if you scroll down. The main 2 problems that I have are : 1) I need the new focused view to be centered in the list(right now as you scroll the layout manager creates the new View and scrolls so that it becomes fully visible but I need it to be centered in the RecyclerView). 2) If you scroll really fast the focus jumps to a view on top of the RecyclerView if the layoutManagers fails to find a new view fast enough (or I'm not quite sure why is this the case). – Tepes Lucian Sep 01 '16 at 19:36
  • `VerticalGridView` from leanback seems to solve my problems but unfortunately the layoutManager it uses is package protected (I can't access anything regarding it;I can copy the source code but I don't like the idea) + it doesn't support `setSpanSizeLookup`. The only way to use it is as an `ListView` + a cell `GridLayout` so that is looks like a grid. The implementation seems to work nice but I'm asking for another more elegant solution (or custom one). I didn't managed to find any libs / leads for this. – Tepes Lucian Sep 01 '16 at 19:40
  • Another option I'm currently using is A DPad Aware Recycler View from here. It handles focus fairly well but the button mashing isn't as good as from the leanback libraries, but it is open source and you can modify and contribute back to it. https://github.com/vganin/dpad-aware-recycler-view – kingargyle Sep 12 '16 at 20:06
  • @kingargyle thanks for the hint. I tried the sample and i agree that it isn't as good as leanback libs but seems to get the job done better that regular recyclerview. Currently I'm using the leanback library `VerticalGridView` as a simple list and seems to work decent enough. I still don't like it plus another downside is that `RecyclerView` default item change animations won't work out of the box. – Tepes Lucian Sep 18 '16 at 09:51
  • 1
    Aha, I met this problem, too. – Zhang Xiang Oct 25 '16 at 06:51
  • @ZhangXiang what did you do in the end? Used the leanback implementation? – Tepes Lucian Jan 31 '17 at 18:06
  • This isn't working did anyone come up with any solution? – niranjan kurambhatti Mar 04 '19 at 05:57