1

I'm using a recyclerView to support an item selection function for an android app, where each item of recyclerView will be a same sized image view. When creating the recyclerView, I already know that the n-th item is the selected one, how do I directly display the n-th item in center or just make it visible?

UPDATE: So I achieved this by first calling linearLayout.scrollToPosition(n-th position) to make the item visible, then adding a addOnGlobalLayoutListener, which will wait for the layout to be complete, then calculate the offset and do a smooth scroll to center.

december
  • 31
  • 3
  • try this http://stackoverflow.com/questions/26875061/scroll-recyclerview-to-show-selected-item-on-top. – D.J Oct 19 '16 at 06:32
  • this works for me [Scroll to nth item ](http://stackoverflow.com/questions/31235183/recyclerview-how-to-smooth-scroll-to-top-of-item-on-a-certain-position) – noobEinstien Oct 19 '16 at 06:41
  • Hi please check this answer .It works for me [] [Scroll to nth item ](http://stackoverflow.com/questions/31235183/recyclerview-how-to-smooth-scroll-to-top-of-item-on-a-certain-position) – noobEinstien Oct 19 '16 at 06:44
  • layoutmanager.scrollToPositionWithOffset helps. – december Oct 19 '16 at 17:14

1 Answers1

1

Use layoutManager.scrollToPositionWithOffset(n-th position, half width of screen).
It automatically scroll to n-th item position.

Sung Min Lee
  • 154
  • 7
  • Thanks! This do work to move the n-th item to visible, but still not in the center.. I guess I'll do more digging from here. :) – december Oct 19 '16 at 17:13
  • Though `half width of screen` makes n-th item to almost center, but you need to calculate exact center values anyway. @december – Sung Min Lee Oct 21 '16 at 01:55