RecyclerVIew自动滚动以显示所有元素,如新闻源等, [英] RecyclerVIew auto scroll to display all the elements as in News Feed etc.,

查看:1103
本文介绍了RecyclerVIew自动滚动以显示所有元素,如新闻源等,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何平滑自动滚动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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆