gpt4 book ai didi

android - RecyclerVIew 自动滚动显示所有元素,如 News Feed 等,

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:15 33 4
gpt4 key购买 nike

如何平滑地自动滚动 RecyclerView 以便用户可以看到 RecyclerView 的所有元素并从头开始再次滚动 - 如新闻提要等。

我知道 smoothScrollToPosition()scrollToPosition() 但它们最终会以过快的速度滚动到最后一个元素。

我希望 RecyclerView 具有动画效果并缓慢移动。

最佳答案

只是为了稍微改进答案,它是自动无限滚动且动画流畅。

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

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);
}
}

关于android - RecyclerVIew 自动滚动显示所有元素,如 News Feed 等,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35773980/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com