我有一个Recyclerview
,ratingbar
list_items.xml
完成Recyclerview
中某个项目的评分后,我会自动滚动到Recyclerview
的另一个项目
滚动非常快,用户无法知道它已滚动。
我实现了平滑滚动,但工作不正常, 第2个项目的第一个滚动顺畅(第1个已经显示),但其他项目滚动很快
LinearLayoutSmoothScroll.java
public class LinearLayoutSmoothScroll extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 100f;
private Context mContext;
public LinearLayoutSmoothScroll(Context context) {
super(context);
mContext=context;
}
public LinearLayoutSmoothScroll(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
mContext=context;
}
public LinearLayoutSmoothScroll(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
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 LinearLayoutSmoothScroll.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.smoothScrollToPosition(rvSurvey,new RecyclerView.State(),new_position);