mobile menu
Site icon
ZidsWorld

The Tech Galaxy

Live support

android logo

RecyclerView Auto Scroll Example in Android Java

Wednesday, February 14, 2024, 7 AM

The recyclerview is an android view which helps load multiple contents dynamically. Sometimes you will want to automatically scroll the recyclerview for your android app, so the code below can help

If you want to scroll your recyclerview automatically, you can use the below code. Just add this code after setting your recyclerview adapter

Timer timerx; 
//first add this global field in your class
//This is the code for auto scroll  
try {
            Timer timer = new Timer();
            timerx = timer;
            final int time = 4000; // it's the delay time for sliding between items in recyclerview
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    try {
                        if (linearLayoutManager.findLastCompletelyVisibleItemPosition() < (topPlayersAdapter.getItemCount() - 1)) {
                            linearLayoutManager.smoothScrollToPosition(ToprecyclerView, new RecyclerView.State(), linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1);
                        } else if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == (topPlayersAdapter.getItemCount() - 1)) {
                            linearLayoutManager.smoothScrollToPosition(ToprecyclerView, new RecyclerView.State(), 0);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, 0, time);
        } catch (Exception e) {
            e.printStackTrace();
        }

Comments

No comments found.