更新时间:2023-10-28 12:45:04
RecyclerView
被设计为可扩展的,因此不需要将 LayoutManager
子类化(如 droidev 建议) 只是为了执行滚动.
RecyclerView
is designed to be extensible, so there is no need to subclass the LayoutManager
(as droidev suggested) just to perform the scrolling.
相反,只需创建一个带有首选项 SNAP_TO_START
的 SmoothScroller
:
Instead, just create a SmoothScroller
with the preference SNAP_TO_START
:
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
现在您设置要滚动到的位置:
Now you set the position where you want to scroll to:
smoothScroller.setTargetPosition(position);
并将该 SmoothScroller 传递给 LayoutManager:
and pass that SmoothScroller to the LayoutManager:
layoutManager.startSmoothScroll(smoothScroller);