SoFunction
Updated on 2025-04-07

Methods to solve various click events of RecyclerView in Android

Perfectly solve RecyclerView click event, long press event, and child click event

Since Google launched RecyclerView, it can completely replace ListView. I personally feel that the only drawback is that the various click events of itemView are not perfect enough. Opinions represent personal opinions only. According to the recent project requirements, I will write a blog to record it. If it can help you, I am honored. Next, the RecyclerView is simply encapsulated, making it more convenient to implement various click events.

We all know that the use of RecyclerView is to create an adapter class, and then create a ViewHolder inner class in the adapter class. What we need to do is to encapsulate these two classes to implement itemView click event, long press event, and child click event.

First of all, my handling method is that for developers, they only need to call the setxxx() method to the adapter, such as setting the click event of the itemView: (...); pass the custom interface to the method. That is, we need to customize an adapter class. Then we will create a class first, named BaseRecylerAdapter. After that, we should also create a BaseViewHolder class and start to do things.

BaseRecylerAdapter class

public abstract class BaseRecyclerAdapter extends <BaseViewHolder>
  implements 
  , {

 private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
 private OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener;
 private OnSubViewClickListener onSubViewClickListener;

 @Override
 public void onBindViewHolder(BaseViewHolder holder, int position) {
  (position);
  (position);
  if (onRecyclerViewItemClickListener != null) {
   (this);
  }
  if (onRecyclerViewItemLongClickListener != null) {
   (this);
  }
  if (onSubViewClickListener != null) {
   (onSubViewClickListener,position);
  }
 }

 public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
   = onRecyclerViewItemClickListener;
 }

 public void setOnRecyclerViewItemLongClickListener(OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener) {
   = onRecyclerViewItemLongClickListener;
 }

 public void setOnSubViewClickListener(OnSubViewClickListener listener){
   = listener;
 }

 @Override
 public void onClick(View v) {
  if (() != null) {
   int position = (int) ();
   (position);
  }
 }

 @Override
 public boolean onLongClick(View v) {
  if (() != null){
   int position = (int)();
   (position);
  }
  return true;
 }

 public interface OnRecyclerViewItemClickListener {
  void onItemClick(int position);
 }

 public interface OnSubViewClickListener{
  void onSubViewClick(View v, int position);
 }

 public interface OnRecyclerViewItemLongClickListener {
  void onItemLongClick(int position);
 }

}

You can see that we have created three interface classes in the class

public interface OnRecyclerViewItemClickListener {
  void onItemClick(int position);
 }

 public interface OnSubViewClickListener{
  void onSubViewClick(View v, int position);
 }

 public interface OnRecyclerViewItemLongClickListener {
  void onItemLongClick(int position);
 }

These three interfaces are callbacks used for click events, and each function can be distinguished by looking at the name. Click callback for itemViewpublic interface OnRecyclerViewItemClickListener, long press of itemViewpublic interface OnRecyclerViewItemLongClickListener, click callback of child Viewpublic interface OnSubViewClickListener. It’s all about click events. How can I send without clicks? Right! So, this class has also been implemented and These two interfaces implement the click event and long press event of itemView.

As you can see, BaseRecyclerAdapter inherits from<BaseViewHolder>, at this time we only need to implementonBindViewHolder This method is enough. To analyze this method.

@Override
 public void onBindViewHolder(BaseViewHolder holder, int position) {
  (position);
  (position);
  if (onRecyclerViewItemClickListener != null) {
   (this);
  }
  if (onRecyclerViewItemLongClickListener != null) {
   (this);
  }
  if (onSubViewClickListener != null) {
   (onSubViewClickListener,position);
  }
 }

It can be seen that this method operates our customized BaseViewHolder class. Next are three empty judgments. That is to say, if we do not set the corresponding click event, we will not initialize the corresponding click event. This way of handling is still very common. The most troublesome thing about handling this click event is position problem, so the trick we use is to set tags on the View object. You can see that View has a methodsetTag(Object obj); We can assign the corresponding position to this tag, and we use the ViewgetTag() The method can get the corresponding position of the view. Click the interface and long press the interface implemented in the BaseRecylerAdapter class to know this operation, the class content is as follows.

 @Override
 public void onClick(View v) {
  if (() != null) {
   int position = (int) ();
   (position);
  }
 }

 @Override
 public boolean onLongClick(View v) {
  if (() != null){
   int position = (int)();
   (position);
  }
  return true;
 }

BaseViewHolder class

public abstract class BaseViewHolder extends  implements {
 private  onSubViewClickListener;
 public BaseViewHolder(View itemView) {
  super(itemView);
  findViewById(itemView);
 }

 /**
   * Parameters required for incoming child click event
   * @param listener Custom interface
   * @param tagPosition tag
   */
 public void setSubViewClickListener( listener, int tagPosition){
   = listener;
  initSubViewClick(tagPosition);
 }

 /**
   * Match controls through id (developers implement it themselves)
   * @param itemView Parent layout
   */
 abstract protected void findViewById(View itemView);

 /**
   * Used to load data (developers can implement it themselves)
   * @param position Current location
   */
 abstract protected void onBind(int position);

 /**
   * Initialize the click event of the child (set tags for the child)
   * @param tagPosition tag
   */
 protected void initSubViewClick(int tagPosition){

 }

 /**
   * Implement forwarding of child click events
   * @param v
   */
 @Override
 public void onClick(View v) {
  if (() != null) {
   int position = (int) ();
   (v,position);
  }
 }
}

This is an abstract class, that is, the abstract methods need to be implemented when using it. For the sake of clear logic, I wrote two abstract methods here

/**
   * Match controls through id (developers implement it themselves)
   * @param itemView Parent layout
   */
 abstract protected void findViewById(View itemView);

 /**
   * Used to load data (developers can implement it themselves)
   * @param position Current location
   */
 abstract protected void onBind(int position);

By looking at the comments, you will be very clear about the role of these two methods, so I won’t talk about it here.

At this point, we have implemented the click and long press events of itemView. Next, we will implement the click events of itemView sub-item.

In the BaseViewHolder class, a View click event interface is also implemented. The clicking method of a child is the same routine as the clicking event of the itemView, using tags. Next, let’s look at an example and you will understand.

public class RecyclerAdapterMyActivity extends BaseRecyclerAdapter{
 private List<MyActivityBean> list;
 public RecyclerAdapterMyActivity(List<MyActivityBean>list){
   = list;
 }


 @Override
 public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View view = (()).inflate(.item_recycler_myactivity_activity,parent,false);
  ViewHolder holder = new ViewHolder(view);
  return holder;
 }


 @Override
 public int getItemCount() {
  return ();
 }

 public class ViewHolder extends BaseViewHolder {
  private TextView tv_name,tv_title,tv_content;
  private Button activityBtnChat;
  private Button activityBtnCancel;
  ViewHolder(View itemView) {
   super(itemView);
  }

  @Override
  protected void findViewById(View itemView) {
   tv_name = (.tv_my_activity_name);
   tv_title = (.tv_my_activity_title);
   tv_content = (.tv_my_activity_content);
   activityBtnChat = (.activity_btn_chat);
   activityBtnCancel = (.activity_btn_cancel);

  }

  @Override
  protected void onBind(int position) {
   MyActivityBean bean = (position);
   tv_name.setText(());
   tv_title.setText(());
   tv_content.setText(());
  }

  @Override
  protected void initSubViewClick(int tagPosition) {
   (tagPosition);
   (tagPosition);
   (this);
   (this);
  }
 }
}

This code is a small piece of code from a recent project. Among them, the adapter class inherits BaseRecyclerAdapter, and the viewHolder class inherits BaseViewHolder. It is particularly important to note that the constructor of ViewHolder must havesuper(itemView); The rest of the methods will be executed according to the correct logic. To implement the child click event of itemView, you need to rewrite the parent class'sinitSubViewClick(int tagPosition); method. The parameter tagPosition is the position where the corresponding itemVIew is in the RecyclerView. Here we add click events for two buttons, first set tags for them, and then set click events.setOnClickListener(this) ;The parameter is passed this because we implement the View in the parent class.onClick(View v); method.

In this way, we have completed all kinds of click events.

The method of use is also very simple, just operate your adapter directly, call(...) Various click events can be easily implemented. Of course, if your requirement is a Touchu event, or a child-mounted event, etc., it can be achieved in a similar way.

at last

There are more than one method, and this way of operation is just a kind of thought for me.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.