我也有同样的问题,但通过自定义 SmoothScroller 设法解决了这个问题
让自定义 LayoutManager 如下
public class CustomLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 50f;
private Context mContext;
public CustomLayoutManager(Context context) {
super(context);
mContext = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
RecyclerView.State state, final int position) {
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(mContext) {
//This controls the direction in which smoothScroll looks
//for your view
@Override
public PointF computeScrollVectorForPosition
(int targetPosition) {
return CustomLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
//This returns the milliseconds it takes to
//scroll one pixel.
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH/displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
}
(文档在上面给出的代码中注释)请将上面的LayoutManager设置为recyerview
CustomLayoutManagerlayoutManager = new CustomLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.smoothScrollToPosition(position);
通过使用自定义布局管理器
scrollToPosition 在我的情况下也可以很好地使用,您可以使用
recyclerView.scrollToPosition(position)
另外,如果您想调整 smoothScrollToPosition 的速度,请覆盖
private static final float MILLISECONDS_PER_INCH = 50f;
在自定义布局管理器中。因此,如果我们将值设置为1f,smoothScrollToPosition 会像 scrollToPosition 一样快。增加值会延迟,减少会加快滚动速度。希望这会有用。