Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter

后端 未结 5 1054
自闭症患者
自闭症患者 2021-02-14 03:54

I have a custom object :

Student.class

public class Student {
  private String name;
  private String age;

  public String getName() {
    return name;
         


        
相关标签:
5条回答
  • 2021-02-14 03:54

    You need to set on onClickListener() on the view of the ViewHolder i.e. itemView in your case. It will call the onClick() method as soon as you click the complete view i.e. root view (itemView in your case)

    you can also set onClickListener() on the children of the root i.e. name and age.

    inside ViewHolder(View itemView) constructor:

    itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext,"clicked="+ getPosition(),Toast.LENGTH_SHORT).show();         
    
                    }
                });
    

    Similarly you can call onLongClickListner() also. And name.setOnClickLisner() on child view too.

    0 讨论(0)
  • 2021-02-14 04:01

    You can implement onClickListener for the parent view like below:

    //holder.view - parent layout inside recyclerView item
    holder.view.setTag(position);
    holder.view..setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (int) v.getTag();
                Student student = students.get(pos);
                // Do your operation
    
            }
        });
    

    OR

    if you want to set onItemClickListener from activity/fragment where recyclerView is initialised, you can follow this link: http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/

    0 讨论(0)
  • 2021-02-14 04:10

    replace this code,

            @Override public void onBindViewHolder (ViewHolder holder,int position){
    
                final Student student = students.get(position);
                holder.name.setText(student.getName());
                holder.age.setText(student.getAge());
    
    
                holder.v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
    
                holder.v.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
            }
    
    0 讨论(0)
  • 2021-02-14 04:14

    Simplest way is holding single ItemClickListener inside your Adapter:

    public class WalletListRecyclerAdapter extends RecyclerView.Adapter<WalletListRecyclerAdapter.ViewHolder> {
        private List<Wallet> wallets;
        private ItemClickListener itemClickListener;
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Wallet wallet = wallets.get(position);
    
            holder.root.setOnClickListener(v -> {
                if (itemClickListener != null)
                    itemClickListener.onClick(holder.root, wallet);
            });
        }
    
        static class ViewHolder extends RecyclerView.ViewHolder {
            View root;
    
            public ViewHolder(View v) {
                super(v);
                root = v;
            }
        }
    
    
        public void setItemClickListener(ItemClickListener itemClickListener) {
            this.itemClickListener = itemClickListener;
        }
    
        public interface ItemClickListener {
            void onClick(View view, Wallet wallet);
        }
    }
    

    In Activity:

    walletsAdapter = new WalletListRecyclerAdapter();
    walletsAdapter.setItemClickListener((view, wallet) -> {
        Intent intent = new Intent(this, WalletActivity.class);
        intent.putExtra(Const.KEY_WALLET, wallet);
        startActivity(intent);
    });
    
    0 讨论(0)
  • 2021-02-14 04:15

    You can set event Onclick for specific audiences:

    Eg:

     holder.layout.setClickListener(new ItemClickListener() {
      @Override public void onClickItem(int pos) {
        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
      }
    
    0 讨论(0)
提交回复
热议问题