athul
athul

Reputation: 179

Automatically scroll RecyclerView horizontally

I'm showing a list of items in recyclerview which has horizontal orientation. Now i want to scroll the recyclerview automatically when it is loaded with items and also it should continue like a loop. How can i achieve this mechanism.

Upvotes: 2

Views: 182

Answers (2)

Longalei
Longalei

Reputation: 502

First,If you want a loop, you can override the recyclerview adapter getItemCount() and return Integer.MAX_VALUE.

Second,If you only want scroll.you can get the Butt's anwser for reference. However,If you want scroll smooth. Luckily.The Recyclerview support it ,you can see the LayoutManager smoothScrollToPosition(),you can custom your SmoothScroller or for a reference with LinearSmoothScroller

Upvotes: 1

Muhammad Usman Butt
Muhammad Usman Butt

Reputation: 545

Use handle for this

final int speed = 5000; // delay time 
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,speed);
    }
   }
   };

 handler.postDelayed(runnable,speed);

Upvotes: 1

Related Questions