RecyclerView.scrollToPosition()是非常奇怪的。例如,假设有一个名为RecyclerView的"rv"。
RecyclerView下,调用rv.scrollToPosition(10),则RecyclerView将将项目10滚动到底部。RecyclerView中,调用rv.scrollToPosition(10),就不会有任何滚动,什么都不会做。RecyclerView的顶部,则调用rv.scrollToPosition(10),RecyclerView将将项目10滚动到顶部。为了帮助理解,请看这张图片。

但是我需要的是,每当我调用它时,RecyclerView就会将假定的位置滚动到当前视图的顶部,就像案例3一样。如何做呢?
发布于 2015-10-25 12:37:28
如果我理解这个问题,你想要滚动到一个特定的位置,但那个位置是适配器的位置,而不是RecyclerView的项目位置。
您只能通过LayoutManager来实现这一点。
做以下事情:
rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter).发布于 2017-04-29 14:44:42
下面的链接可以解决您的问题:
https://stackoverflow.com/a/43505830/4849554
只需使用首选项SNAP_TO_START创建一个SNAP_TO_START:
RecyclerView.SmoothScroller smoothScroller = new
LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};现在,将要滚动到的位置设置为:
smoothScroller.setTargetPosition(position);并将该SmoothScroller传递给LayoutManager:
layoutManager.startSmoothScroll(smoothScroller);发布于 2018-04-17 13:26:18
如果要滚动到特定位置,并且该位置是适配器的位置,则可以使用StaggeredGridLayoutManager scrollToPosition
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
staggeredGridLayoutManager.scrollToPosition(10);
recyclerView.setLayoutManager(staggeredGridLayoutManager);https://stackoverflow.com/questions/33328806
复制相似问题