Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Horizontal Auto scroll in Recycler view

Tags:

android

I have two values in list and displaying that in horizontal list view using Recycler view. Here I need to auto scroll the horizontal list unlimited. I tried with the below code but no result.

HorizontalScrollView: auto-scroll to end when new Views are added?

like image 454
sathish Avatar asked Dec 06 '22 17:12

sathish


1 Answers

please check the solution here. https://github.com/ritesh-bhavsar86/StockAutoScroll

first create runnable:

final int duration = 10;
final int pixelsToMove = 30;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {

    @Override
    public void run() {
        rv_autoScroll.smoothScrollBy(pixelsToMove, 0);
        mHandler.postDelayed(this, duration);
    }
};

then after setadapter() to the recyclerView use following:

rv_autoScroll.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int lastItem = layoutManager.findLastCompletelyVisibleItemPosition();
            if(lastItem == layoutManager.getItemCount()-1){
                mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                Handler postHandler = new Handler();
                postHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rv_autoScroll.setAdapter(null);
                        rv_autoScroll.setAdapter(madapter);
                        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                    }
                }, 2000);
            }
        }
    });
    mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);

rv_autoScroll is recyclerview

and

layoutmanager is LayoutManager which set to recyclerview

like image 113
Ritesh Bhavsar Avatar answered Dec 10 '22 12:12

Ritesh Bhavsar