0

I'm implementing an Android TV app and I'm using HorizontalGridView from the Leanback library. I have a custom layout.

I have to scroll the HorizontalGridView to specific position, after activity is created, but unfortunately the scrollToPositio(position) method is not working on this layout at all. It just do nothing. I found, that when I specifically set the layout manager to LinearLayoutManager it works. But the problem is, that when I'm not using leanback default HorizontalGridView LayoutManager, there is a problem with focusing next items using D-pad.

Basically if I use normal RecyclerView, the control with D-pad is not working as expected, so I decided to go with leanback implementation, where this problem is solved, but so far I cannot make it work with scrollToPosition method.

Any ideas?

Snippet of my code:

Layout:

<android.support.v17.leanback.widget.HorizontalGridView
        android:id="@+id/photo_gallery_recycler"
        android:layout_width="match_parent"
        android:layout_height="@dimen/gallery_image_size"
        android:clipChildren="false"
        app:itemView="@{viewModel.photoItemView}"
        app:items="@{viewModel.photosUrl}"/>

Code [Kotlin]:

binding.photoGalleryRecycler.scrollToPosition(position)
binding.photoGalleryRecycler.getChildAt(position)?.requestFocus()

And I also tried some hacks like this:

// save default leanback layout manager    
var defaultLayoutManager = binding.photoGalleryRecycler.layoutManager
// set LinearLayoutManager
binding.photoGalleryRecycler.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
// scroll to position and request focus
binding.photoGalleryRecycler.scrollToPosition(position)
binding.photoGalleryRecycler.getChildAt(position)?.requestFocus()
// set default layout manager back to the view
binding.photoGalleryRecycler.layoutManager = defaultLayoutManager
Stepan Sanda
  • 2,322
  • 7
  • 31
  • 55
  • Could you describe more with reference screen. – Jay Rathod Mar 26 '16 at 09:54
  • There is actually just one view which in mobile version can be normal RecyclerView with Horizontal LinearLayoutManager. Unfortunately the control with D-pad on TVs has some troubles with focusing right items in RecyclerView, so it was necessary to use implementation from leanback library where they fixed this problem with counting right next item focus, but for some reason the scrollToPosition is not working. – Stepan Sanda Mar 26 '16 at 22:50
  • 1
    [setSelectedPosition(int)](http://developer.android.com/intl/zh-cn/reference/android/support/v17/leanback/widget/HorizontalGridView.html#setSelectedPosition(int)) – Dhinakaran Thennarasu Mar 28 '16 at 06:03
  • @Dhina: Your answer is working! Thank you so much.. Can you add it as answer so I can check it as answer? – Stepan Sanda Mar 28 '16 at 20:18

1 Answers1

3

you need to be using setSelectedPosition(position)

If animation is required you can try setSelectedPositionSmooth(position)

setSelectedPosition Developer docs.

Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39