Use the same RecyclerView adapter twice

ojciecMateusz :

Is it possible to use the same RecyclerView adapter twice with other getItemCount? Once I need only 6 items, and in other activity I need all of them.

 @Override
public int getItemCount() {
    return 6;
}
Nilesh Rathod :

Try this way

You can pass a boolean flag in your adapter class

and based on that boolean variable you can returns item in your getItemCount() method

SAMPLE CODE

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<String> arrayList;
    boolean flag;

    public MyAdapter(Context mContext, ArrayList<String> arrayList, boolean flag) {
        this.mContext = mContext;
        this.arrayList = arrayList;
        this.flag = flag;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return flag ? 6 : arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=410716&siteId=1