首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RecyclerVIew自动滚动以显示所有元素,如新闻馈送等。

问RecyclerVIew自动滚动以显示所有元素,如新闻馈送等。
EN

Stack Overflow用户
提问于 2016-03-03 21:48:09
回答 8查看 21K关注 0票数 11

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

我知道smoothScrollToPosition()和scrollToPosition(),但它们滚动到最后一个元素的速度太快了。

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

EN

回答 8

Stack Overflow用户

发布于 2017-02-02 16:03:11

只是为了改进一下答案,它是自动无限滚动,动画流畅。

代码语言:javascript
复制
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

代码语言:javascript
复制
recyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

控制你的动画改变MILLISECONDS_PER_INCH值。

代码语言:javascript
复制
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);
    }
}
票数 27
EN

Stack Overflow用户

发布于 2016-03-03 22:09:18

我认为这是最好的解决方案。

代码语言:javascript
复制
    final int speedScroll = 150;
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        int count = 0;
        @Override
        public void run() {
            if(count < list.size()){
                recyclerView.scrollToPosition(count++);
                handler.postDelayed(this,speedScroll);
            }


        }
    };

    handler.postDelayed(runnable,speedScroll);
票数 12
EN

Stack Overflow用户

发布于 2019-07-03 22:29:16

这是自动滚动RecyclerView及其100%工作的最佳方式:

代码语言:javascript
复制
  RecyclerView recyclerView = findViewById(R.id.rv_id);

  final int time = 4000; // it's the delay time for sliding between items in recyclerview

    final Adapter adapter = new Adapter(dataItems);
    recyclerView.setAdapter(adapter);

    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(linearLayoutManager);

    //The LinearSnapHelper will snap the center of the target child view to the center of the attached RecyclerView , it's optional if you want , you can use it
    final LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
    linearSnapHelper.attachToRecyclerView(recyclerView); 

    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {

            if (linearLayoutManager.findLastCompletelyVisibleItemPosition() < (adapter.getItemCount() - 1)) {

                linearLayoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1);
            }

            else if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == (adapter.getItemCount() - 1)) {

                linearLayoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), 0);
            }
        }
    }, 0, time);
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35773980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档