RecyclerVIew 自动滚动以显示新闻提要等中的所有元素, [英] RecyclerVIew auto scroll to display all the elements as in News Feed etc.,
问题描述
如何平滑地自动滚动 RecyclerView
以便用户可以看到 RecyclerView 的所有元素并从头开始再次滚动 - 如新闻提要等
How to auto scroll RecyclerView
smoothly so that user can see all the elements of the RecyclerView and scroll again from the start - as in News Feed etc.
我知道 smoothScrollToPosition()
和 scrollToPosition()
但它们最终滚动到最后一个元素的速度太快了.
I know smoothScrollToPosition()
and scrollToPosition()
but they would just end up scrolling too fast to the last element.
我希望 RecyclerView 具有动画效果并缓慢移动.
I want the RecyclerView to be animated and move slowly.
推荐答案
只是为了稍微改进一下答案,它是具有平滑动画的自动滚动无穷大.
final int speedScroll = 1200;
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
int count = 0;
boolean flag = true;
@Override
public void run() {
if(count < adapter.getItemCount()){
if(count==adapter.getItemCount()-1){
flag = false;
}else if(count == 0){
flag = true;
}
if(flag) count++;
else count--;
recyclerView.smoothScrollToPosition(count);
handler.postDelayed(this,speedScroll);
}
}
};
handler.postDelayed(runnable,speedScroll);
这正是您的答案,但如果您链接到更流畅的动画,请使用 LayoutManager
This is exactly your answer but if you link to more smooth animation then use LayoutManager
recyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
控制动画改变 MILLISECONDS_PER_INCH 值.
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(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()) {
private static final float MILLISECONDS_PER_INCH = 200f;
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return CustomLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
这篇关于RecyclerVIew 自动滚动以显示新闻提要等中的所有元素,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!