Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

android - RecyclerVIew auto scroll to display all the elements as in News Feed etc.,

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.

I know smoothScrollToPosition() and scrollToPosition() but they would just end up scrolling too fast to the last element.

I want the RecyclerView to be animated and move slowly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just to improve on the answer a little, it's auto scrolling infinity with smooth animation.

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

This is exactly your answer but if you link to more smooth animation then use LayoutManager

recyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

Control you animation changing MILLISECONDS_PER_INCH value.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...