简体   繁体   中英

How and where to set an OnClickListener listener for two RecyclerView instances

I have two RecyclerView in my Activity . I set an OnClickListener for one of them and implement the onItemClick method.

If I want to set OnClickListener and implement onItemClick for the second RecyclerView , how do I achieve this?

The recommended way to add listeners to a RecyclerView is to make the ViewHolder implement the listener and then register the listener on the View that is passed to the ViewHolder constructor. Example :

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>    

    public class MyViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
        private TextView textView;

        public MyViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
            textView = (TextView)view.findViewById(R.id.tv_data);
        }

        @Override
        public void onClick(View v) {
            //do something on click using the position
             int adapterPosition = getAdapterPosition();
       }
   }  
}

If you have two RecyclerView classes, you need to set the listener in the second ViewHolder implementation in a similar fashion.

Note : While there are multiple ways in which one can register a listener for a RecyclerView , the above approach defines the ViewHolder implementaion as an inner class in the adapter class and also ensures that only classes that need to know about clicks contain the code for handling them.

You can set click listner on both of the RecyclerView by RecyclerviewAdapter class.

Inside onBindViewHolder() of Adapter Class. (Same code for both of the RecyclerView Adapter class)

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    LoadDataResult listPotn = list.get(position);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Call intent or call method of Activity from here.

            Context context = v.getContext();
            Intent intent = new Intent(context , Excercise.class);
            context.startActivity(intent);
        }
    });
}

Or,

public class ViewHolder extends RecyclerView.ViewHolder {
TextView product_name;

ViewHolder(View itemView) {
    super(itemView);
    product_name = (TextView) itemView.findViewById(R.id.product_name);
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int itemPosition = getLayoutPosition();
            Toast.makeText(getApplicationContext(), itemPosition + ":" + String.valueOf(product_name.getText()), Toast.LENGTH_SHORT).show();
        }
    });
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM