Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add click listener to Generic RecyclerView Adapter

Edit

As It is a genericAdapter not simple one and I know the methods to add click listener. And it is not a good practice to do this in onCreateViewHolder. So that's why I need a better suggestion

I have created a Generic Adapter for RecyclerView in android. Now I want some suggestion to improve it. And how could I add clickListener to it.

GenericAdapter.java

public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<T> items;
    private OnRecyclerItemClicked onRecyclerItemClicked;

    public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent);

    public abstract void onBindData(RecyclerView.ViewHolder holder, T val);

    public GenericAdapter(Context context, ArrayList<T> items){
        this.context = context;
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = setViewHolder(parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindData(holder,items.get(position));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void addItems( ArrayList<T> savedCardItemz){
        items = savedCardItemz;
        this.notifyDataSetChanged();
    }

    public T getItem(int position){
        return items.get(position);
    }

    public void setOnRecyclerItemClicked(OnRecyclerItemClicked onRecyclerItemClicked){
        this.onRecyclerItemClicked = onRecyclerItemClicked;
    }

    public interface OnRecyclerItemClicked{
        void onItemClicked(View view,int position);
    }
}

And Call it like

adapter = new GenericAdapter<MyModelClass>(context,listOfModelClass) {
                @Override
                public RecyclerView.ViewHolder setViewHolder(ViewGroup parent) {
                    final View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_view, parent, false);
                    AViewHolder viewHolder = new AViewHolder(context, view);
                    return viewHolder;
                }

                @Override
                public void onBindData(RecyclerView.ViewHolder holder1, MyModelClass val) {
                        MyModelClass currentCard = val;

                        AViewHolder holder = (AViewHolder)holder1;
                        holder.cardNumber.setText(currentCard.getDisplayNumber());
                        holder.cardHolderName.setText(currentCard.getCardHolderName());
                }
            };
            mRecyclerView.setAdapter(adapter);

Now how and where could I add a click listener. As adding click listener to onBindData is an overhead. Need suggestion.

like image 801
Zar E Ahmer Avatar asked Aug 02 '16 07:08

Zar E Ahmer


1 Answers

Have you tried adding a ViewHolder and add the clicklistener to it

Now GenericAdapter.java.

public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<T> items;
    private OnRecyclerItemClicked onRecyclerItemClicked;

    public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent , OnRecyclerItemClicked onRecyclerItemClicked);

    public abstract void onBindData(RecyclerView.ViewHolder holder, T val);

    public abstract OnRecyclerItemClicked onGetRecyclerItemClickListener();

    public GenericAdapter(Context context, List<T> items){
        this.context = context;
        this.items = items;
        onRecyclerItemClicked = onGetRecyclerItemClickListener();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = setViewHolder(parent , onRecyclerItemClicked);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindData(holder,items.get(position));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void setItems( ArrayList<T> savedCardItemz){
        items = savedCardItemz;
        this.notifyDataSetChanged();
    }

    public T getItem(int position){
        return items.get(position);
    }

    public interface OnRecyclerItemClicked{
        void onItemClicked(View view,int position);
    }
}

And calling it like this

GenericAdapter<CreditCardItemBO> adaptering = new GenericAdapter<CreditCardItemBO>(mContext,new ArrayList<CreditCardItemBO>()) {
            @Override
            public RecyclerView.ViewHolder setViewHolder(ViewGroup parent, OnRecyclerItemClicked onRecyclerItemClicked) {
                final View view = LayoutInflater.from(mContext).inflate(R.layout.item_save_credit_card, parent, false);
                CreditCardViewHolder viewHolder = new CreditCardViewHolder(mContext, view,onRecyclerItemClicked);
                return viewHolder;
            }

            @Override
            public void onBindData(RecyclerView.ViewHolder holder, CreditCardItemBO val) {

            }

            @Override
            public OnRecyclerItemClicked onGetRecyclerItemClickListener() {
                return new OnRecyclerItemClicked() {
                    @Override
                    public void onItemClicked(View view, int position) {

                    }
                };
            }
        };
like image 84
Santa Teclado Avatar answered Oct 18 '22 06:10

Santa Teclado