5

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?

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
sathish
  • 51
  • 1
  • 1
  • 3
  • This answer can be helpful and its 100% working : [Auto Scroll RecyclerView](https://stackoverflow.com/a/56872365/6842344) – ParSa Jul 03 '19 at 14:38

2 Answers2

17

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

Ritesh Bhavsar
  • 1,343
  • 1
  • 9
  • 19
0
trainigItemRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if(lastItem == linearLayoutManager.getItemCount()-1){
                    mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                    Handler postHandler = new Handler();
                    postHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            trainigItemRV.setAdapter(null);
                            trainigItemRV.setAdapter(productsTrainingItemAdapter);
                            mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                        }
                    }, 2000);
                }
            }
        });
        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);

This Works For me..
Alok Singh
  • 640
  • 4
  • 14