Add click listener to Generic RecyclerView Adapter

北城以北 提交于 2019-12-07 02:50:51

问题


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.


回答1:


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) {

                    }
                };
            }
        };



回答2:


You can add the listener in the activity you have declared the RecyclerView:

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, 
   new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
                //Handle the action
            }
        }

        @Override
        public void onLongClick(View view, int position) {

        }

    }));



回答3:


I have implemented ViewHolder in Santa solution like this:

public class EmployeeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private TextView employeeName;
    private BaseRecyclerAdapter.OnRecyclerItemClicked mOnRecyclerItemClicked;

    public EmployeeViewHolder(View view, BaseRecyclerAdapter.OnRecyclerItemClicked onRecyclerItemClicked) {
        super(view);
        employeeName = (TextView) view.findViewById(R.id.employee_name);
        mOnRecyclerItemClicked = onRecyclerItemClicked;
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        mOnRecyclerItemClicked.onItemClicked(view, getAdapterPosition());
    }

    public TextView getEmployeeName() {
        return employeeName;
    }
}

If you need the context ad it in to constructor as a parameter.




回答4:


I have created a generic adapter, the aar and the sources are at this url:

it's easy usage:

RecyclerAdapter<Customer> mAdapter = new RecyclerAdapter(customerList, CustomerViewHolder.class);

add attach listener (lambda sample)

RecyclerItemClickListener.affectOnItemClick(mRecycler, (position, view1) -> { 
 //action
});

see Url link more details



来源:https://stackoverflow.com/questions/38714460/add-click-listener-to-generic-recyclerview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!