I'm using linear layout manager and RecyclerView
with a LinearLayout Manager
to populate some list of items. When I'm displaying the recyclerview
for the first time and I use:
linearLayoutManager.scrollToPosition(desiredindex);
it scrolls to the top exactly where I want.
Now here is the tricky part - When I'm scrolling to top of recyclerview
(i.e. new items indices will be lower than the desiredindex
) and I call:
linearLayoutManager.scrollToPosition(desiredindex);
It still works fine, but when the recyclerview
has been scrolled beyond the desiredindex,
the recycler view
scrolls such that the desiredindex
item comes to the bottom rather than on top, but I want the tile to scroll to the top not the bottom.
In the above code we have added recycler view to window manger as relative parent layout and add FloatingActionButton. FloatingActionButton supports CoordinatorLayout. So we have used parent layout is CoordinatorLayout. When you click on FloatingActionButton, it will scroll to top position.
For these methods to work, the LayoutManager of the RecyclerView needs to have implemented these methods, and LinearLayoutManager does implement these in a basic manner, so you should be good to go. @SoliTawako I guess scrollTo(int x, int y) you can also scroll view, but in a more fine-tuned way.
Use the setHasFixedsize method If the height of our RecyclerView items is fixed then we should use the setHasFixedsize method in our XML of our card item. This will fix the height of our RecyclerView item and prevent it from increasing or decreasing the size of our Card Layout.
Use scrollToPositionWithOffset
like this:
linearLayoutManager.scrollToPositionWithOffset(desiredindex, 0);
scrolltopositionwithoffset(position, offset) forces the indicated item visible with indicated offset. The offset is distance from the top of RecyclerView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With