Expiredmind
Expiredmind

Reputation: 808

Endless RecyclerView, that repeat data when goes to the end

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.

Upvotes: 6

Views: 5450

Answers (1)

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

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

Upvotes: 10

Related Questions