I want to make RecyclerView based list that user can scroll infinitely.There is only couple of items on it, but whenever user scroll to the end, first items of the list appearing and so one. This feature can work in both directions of scrolling.
Asked
Active
Viewed 5,125 times
6
-
1https://stackoverflow.com/questions/29693593/circular-auto-scrolling-horizontalscrollview-android – Ashish Kumar Jun 18 '18 at 14:52
-
That work thanks! :) – Expiredmind Jun 19 '18 at 06:33
-
Welcome bro :-) – Ashish Kumar Jun 19 '18 at 07:20
1 Answers
10
You are lucky, I did it a couple of days ago.
The trick in my solution was to override the getItemCount()
of the adapter
so that it works with Integer.MAX_VALUE
value.
The getItemCount()
is used by the recyclerview
to determinate how many items there are in the list, and if it returns always MAX_VALUE
, the list is pretty much infinite.
This is my example:
Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mlayout);
RecyclerView myRv = findViewById(R.id.myRv);
ArrayList<MyObject> objectList = new ArrayList<>();
objectList = retrieveObjectList();
myRv.setLayoutManager(new SlowLayoutManager(myActivity.this));
MyAdapter myAdapter = new MyAdapter(objectList);
myRv.setAdapter(myAdapter);
}
Adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<MyObject> myObjects;
public MyAdapter(ArrayList<MyObject> myObjects) {
this.myObjects = myObjects;
}
//used to retrieve the effective item position in list
public int getActualItemCount() {
if (myObjects == null) {
myObjects = new ArrayList<>();
}
return myObjects.size();
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyAdapter.MyViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MessagesAdapter.MessagesViewHolder holder, int position) {
if (myObjects.size() == 0) {
holder.bind(null);
} else {
MyObject myObject = myObjects.get(position % myObjects.size());
holder.bind(SMSMessage);
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView myTv;
MessagesViewHolder(View itemView) {
super(itemView);
myTv = itemView.findViewById(R.id.myTv);
}
void bind(MyObject myObject) {
if (myObject != null) {
myTv.setText(myObject.getProperty());
} else {
myTv.setText("");
}
}
}
}
I use this way (obj I changed names so you can fill them with yours, since some of mine were similar to native ones).
If you have any question, ask freely

Pier Giorgio Misley
- 5,305
- 4
- 27
- 66
-
Thats worked, mr Ashish Kumar commented me with the post with the same solution, little bit hacky but work very well, thanks! – Expiredmind Jun 19 '18 at 06:35
-
-
-
-
-
@DhanuK looks like the problem is that you didn't manage correctly the "empty list" case. could you post your code on something like github and share it so I can check? – Pier Giorgio Misley Sep 25 '19 at 07:34
-
Some how Integer.MAX_VALUE does not work properly with reverseLayout and StackfromEnd True. – ADM Jan 19 '21 at 12:49