I wish to understand why I can't seem to use the scrollToPositionWithOffset method on a LinearLayoutManager? Please see the image for what I mean :
A little background:
The first line in the image (with scrollToPosition
) is scrolling the RecyclerView to make the position (in this case 50) visible - this often means that the selected position shows up at the bottom of the visible RecyclerView (where position 50 first becomes visible after 'scrolling'). Whereas I want to always show it at the top. From my research, a some-what solution seems to be to use this scrollToPositionWithOffset
method (Scroll RecyclerView to show selected item on top)
Interestingly, I was able to achieve what I wanted by customizing SmoothScroller of LinearLayoutManager, but my dataset is huge so speed of 'smooth scrolling' is an issue, and I can't seem increase the speed enough without causing other issues.
In short, I'm hoping that scrollToPositionWithOffset will do the trick for me. However, I don't know how to access the method.
You need to cast the LayoutManager
returned from RecyclerView.getLayoutManager()
.
(recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset
Here is how:
recyclerView.apply {
(layoutManager as LinearLayoutManager).scrollToPositionWithOffset(itemPosition, offsetValue)
}
Note that offsetValue could be X if your list/recycleView has horizontal orientation or Y if your list/recycleView has vertical orientation.
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