Search code examples
javaandroidandroid-recyclerviewadaptermvp

Made a custom Adapter for my RecyclerView with a List


I want to make a custom Adapter for my RecyclerView because I need to use a custom method inside who will init my List later when my presenter will be ready:

    public void setList(List<Object> data){
        this.data = data;
    }

This is my not custom interface for my Adapter without implementation.

final class AdapterReviews extends RecyclerView.Adapter<AdapterReviews.ReviewViewHolder> {}

The question is how should be the interface for my custom Adapter?


Solution

  • A basic RecyclerView CustomAdapter looks like this

    public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private final Context context;
        ArrayList<String> list = new ArrayList<>();
    
        public CustomAdapter(Context context, ArrayList<String> list) { // you can pass other parameters in constructor
            this.context = context;
            this.list = list;
        }
    
        private class CustomAdapterItemView extends RecyclerView.ViewHolder {
            final TextView textView;
    
            CustomAdapterItemView(final View itemView) {
                super(itemView);
                textView = (TextView) itemView;
            }
    
            void bind(int position) {
                textView.setText("");
            }
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new CustomAdapterItemView(LayoutInflater.from(context).inflate(R.layout.item_color, parent, false));
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((CustomAdapterItemView) holder).bind(position);
        }
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    
        // Add your other methods here
    }