首先定義一個(gè)滾動(dòng)輔助類
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(_mActivity) {
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return super.calculateSpeedPerPixel(displayMetrics);
}
@Override
protected int getHorizontalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
在RecyclerView設(shè)置LayoutManager時(shí)復(fù)寫方法
mRvBloodBags.setLayoutManager(new LinearLayoutManager(_mActivity) {
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
});
在需要滾動(dòng)的地方調(diào)用方法
mRvBloodBags.smoothScrollToPosition(pos);