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 :
Whereas I want to always show it at the top. From my research, a some-what solution seems to be to use this
scrollToPositionWithOffset<\/code> method (
However, I don't know how to access the method.
您需要投射从RecyclerView.getLayoutManager()
返回的LayoutManager
。
(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.
To provide a little more context to the other answers, scrollToPositionWithOffset()<\/code> is not a method of LayoutManager (the base class), but one of LinearLayoutManager (and its subclasses).
My assumption is that the reasoning here was that such a method is not necessarily applicable (or could be ambiguous) for other LayoutManagers. Suppose we have a more "exotic" layout manager which does not organize items as a simple list, but rather in a circular shape. What should the offset do in such a case? Does it represent an extra amount radians\/degrees to add to the scroll? Or rather something else?
Please correct me if I'm wrong, but I think that's why only LinearLayoutManagers have access to the method, hence requiring a cast of the RecyclerView's LayoutManager.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.