一行代码解决:
RecyclerViewScrollHelper.scrollToPosition(recyclerView,position);
RecyclerViewScrollHelper源码:
public class RecyclerViewScrollHelper {
public static void scrollToPosition(RecyclerView recyclerView, int position){
RecyclerView.LayoutManager manager1 = recyclerView.getLayoutManager();
if (manager1 instanceof LinearLayoutManager) {
LinearLayoutManager manager = (LinearLayoutManager) manager1;
final TopSmoothScroller mScroller = new TopSmoothScroller(recyclerView.getContext());
mScroller.setTargetPosition(position);
manager.startSmoothScroll(mScroller);
}
}
public static class TopSmoothScroller extends LinearSmoothScroller {
TopSmoothScroller(Context context) {
super(context);
}
@Override
protected int getHorizontalSnapPreference() {
return SNAP_TO_START;
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}