我需要在我的RecyclerView
上执行一些拖动类似的操作,当我到达RecyclerView
的顶端或底端时,我想要顺利滚动它...
我这样做如下:我发布此runnable进行滚动,直到用户不再接近RecyclerView
的开头/结尾。
mScroll = new Runnable() {
@Override
public void run() {
rvImages.smoothScrollBy(0, mScrollDist.get());
mHandler.postDelayed(mScroll, 100);
}
};
我需要定义的问题是,滚动所需的距离应该在100毫秒内完成,这样整体滚动就会很平滑。
我怎样才能做到这一点?
答案 0 :(得分:0)
使用自定义LinearLayoutManager
ClassBasedView
自定义Scrool速度
public class MyCustomLayoutManager extends LinearLayoutManager {
//We need mContext to create our LinearSmoothScroller
private Context mContext;
public MyCustomLayoutManager(Context context) {
super(context);
mContext = context;
}
//Override this method? Check.
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
RecyclerView.State state, int position) {
//Create your RecyclerView.SmoothScroller instance? Check.
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(mContext) {
//Automatically implements this method on instantiation.
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
//What is PointF? A class that just holds two float coordinates.
//Accepts a (x , y)
//for y: use -1 for up direction, 1 for down direction.
//for x (did not test): use -1 for left direction, 1 for right
//direction.
//We let our custom LinearLayoutManager calculate PointF for us
return MyCustomLayoutManager.this.computeScrollVectorForPosition
(targetPosition);
}
};
//Docs do not tell us anything about this,
//but we need to set the position we want to scroll to.
smoothScroller.setTargetPosition(position);
//Call startSmoothScroll(SmoothScroller)? Check.
startSmoothScroll(smoothScroller);
}
}
活动:
//Make an instance variable at the top of you custom LayoutManager
private static final float MILLISECONDS_PER_INCH = 50f;
.
.
.
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(mContext) {
.
.
.
//The holy grail of smooth scrolling
//returns the milliseconds it takes to scroll one pixel.
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};