Scrolling RecyclerView Scrolling to a position always on top

I use the Line Layout Manager and RecyclerView with the LinearLayout Manager to populate some list of items. When I first display recyclerview and use:

 linearLayoutManager.scrollToPosition(desiredindex); 

he scrolls to the top exactly where I want.

Now here is the hardest part: when I scroll to the top of the recyclerview (i.e. the indexes of new items will be lower than the desiredindex ), I call:

 linearLayoutManager.scrollToPosition(desiredindex); 

It still works fine, but when the recyclerview scrolls beyond the desiredindex, the recycler view scrolls so that the desiredindex element falls down, not top, but I want the tile to scroll up and not down.

+5
source share
1 answer

Use scrollToPositionWithOffset as follows:

 linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0); 

scrolltopositionwithoffset (position, offset) makes the specified element visible with the specified offset. Offset is the distance from the top of the RecyclerView .

+21
source

All Articles