本文介绍了如何控制recyclerView.smoothScrollToPosition(position)的滚动速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个回收站视图,我想平滑向下滚动,然后以编程方式向上滚动到它,以便向用户显示其中的完整内容.
I have a recycler view, and I want a smooth scrolldown and then scrollup to it programatically to show the complete content in it to user.
我可以通过以下方式做到这一点:
I can do this by:
final int height=recyclerView.getChildAt(0).getHeight();
recyclerView.smoothScrollToPosition(height);
recyclerView.postDelayed(new Runnable() {
public void run() {
recyclerView.smoothScrollToPosition(0);
}
},200);
但是我想要放慢滚动速度,以使完整的内容清晰可见.
But what I want is to slow down the scrolling speed, so that the complete content gets visible clearly.
推荐答案
只需对答案进行一些改进:
Just to improve on the answer a little:
public class SpeedyLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 5f; //default is 25f (bigger = slower)
public SpeedyLinearLayoutManager(Context context) {
super(context);
}
public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return super.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
然后将SpeedyLayoutManager设置为您的RecyclerView:
And then set SpeedyLayoutManager to your RecyclerView:
recyclerView.setLayoutManager(new SpeedyLinearLayoutManager(context, SpeedyLinearLayoutManager.VERTICAL, false);
这篇关于如何控制recyclerView.smoothScrollToPosition(position)的滚动速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!