RecyclerVIew自动滚动显示新闻Feed等中的所有元素,

问题描述 投票:7回答:4

如何平滑地自动滚动RecyclerView,以便用户可以看到RecyclerView的所有元素并从头开始再次滚动 - 如同News Feed等。

我知道smoothScrollToPosition()scrollToPosition(),但他们最终会滚动太快到最后一个元素。

我希望RecyclerView能够动画并且移动缓慢。

android scroll android-recyclerview recycler-adapter
4个回答
18
投票

只是为了改善答案,它是自动滚动无限和平滑动画。

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

9
投票

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

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

1
投票

经过试验和错误,这对我来说是完美的

    final RecyclerView mRecyclerViewr;
    final ArrayList<String> stringArrayData = new ArrayList<String>
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
    mRecyclerViewr.setLayoutManager(linearLayoutManager);
    CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), topPriceBarList);
    mRecyclerViewr.setAdapter(customAdapter);

        // Auto Scroll Left To Right
        final int scrollSpeed = 100;   // Scroll Speed in Milliseconds
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            int x = 15;        // Pixels To Move/Scroll
            boolean flag = true;
            // Find Scroll Position By Accessing RecyclerView's LinearLayout's Visible Item Position
            int scrollPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();  
            int arraySize = stringArrayData.size();  // Gets RecyclerView's Adapter's Array Size

            @Override
            public void run() {
                if (scrollPosition < arraySize) {
                    if (scrollPosition == arraySize - 1) {
                        flag = false;
                    } else if (scrollPosition <= 1) {
                        flag = true;
                    }
                    if (!flag) {
                        try {
                            // Delay in Seconds So User Can Completely Read Till Last String
                            TimeUnit.SECONDS.sleep(1); 
                            mRecyclerViewr.scrollToPosition(0);  // Jumps Back Scroll To Start Point
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    // Know The Last Visible Item
                    scrollPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();

                    mRecyclerViewr.smoothScrollBy(x, 0);
                    handler.postDelayed(this, scrollSpeed);
                }
            }
        };
        handler.postDelayed(runnable, scrollSpeed);

这将自动滚动您的RecyclerView到最后,等待第二个(So User Can Read Till End)并跳转/滚动回RecyclerView的数组列表中的第一个字符串。如果你想在Posexswpoi的方向和负方向自动滚动

You Just Need To Change The Condition Instead Of Using if(!flag) You Need To Set Value Of x in it

0
投票

在将recyclerview设置为适配器之后,您也可以通过这种方式实现它

    if (flag) x = 15;
    else x= -15;  // For Auto Scroll To Negative i.e Left Direction

0
投票

这是Auto Scroll RecyclerView及其100%工作的最佳方式:

final int duration = 10;
final int pixelsToMove = 263;
final Handler mHandler = new Handler(Looper.getMainLooper());

final Runnable SCROLLING_RUNNABLE = new Runnable() {
        @Override
        public void run() {
            recyclerView.smoothScrollBy(pixelsToMove, 0);
            mHandler.postDelayed(this, duration);
        }
    };

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int lastItem = horizontalLayoutManager.findLastCompletelyVisibleItemPosition();
            if (lastItem == horizontalLayoutManager.getItemCount() - 1) {
                mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                Handler postHandler = new Handler();
                postHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        quickTips.setAdapter(null);
                        quickTips.setAdapter(adapter);
                        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                    }
                }, 2000);
            }
        }
    });
    mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
© www.soinside.com 2019 - 2024. All rights reserved.