Android XRecyclerView Simplest item Click Event Handling

  • 2021-10-25 07:56:33
  • OfStack

In the past, PullToRefresh was used, but later I thought it was too out. Nowadays, many people use RecyclerView, which is very simple in usage and diversified in layout, mainly with waterfalls. Only then did we know that RecyclerView. LayoutManager is really powerful.

But when it comes to addHeaderView, RecyclerView has not been implemented, so I used XRecyclerView. In fact, it is encapsulated again on the basis of RecyclerView, which is quite good to use.

Here say 1, the correct use of XRecyclerView click item to do event processing. In fact, it is to click on item in RecyclerView. ViewHolder, so one problem in design is how to use it simply.

In general, we will directly set an id= "@ + id/…" in the outermost layer of the item layout, and then use holder.**.setOnClickListener () for event handling in onBindViewHolder. See if you do this. If so, then continue to look down and teach you simple.

Simple use of item click events

1. First look at how the source code of RecyclerView. ViewHolder is written


/**
 * A ViewHolder describes an item view and metadata about its place within the RecyclerView.
 *
 * <p>{@link Adapter} implementations should subclass ViewHolder and add fields for caching
 * potentially expensive {@link View#findViewById(int)} results.</p>
 *
 * <p>While {@link LayoutParams} belong to the {@link LayoutManager},
 * {@link ViewHolder ViewHolders} belong to the adapter. Adapters should feel free to use
 * their own custom ViewHolder implementations to store data that makes binding view contents
 * easier. Implementations should assume that individual item views will hold strong references
 * to <code>ViewHolder</code> objects and that <code>RecyclerView</code> instances may hold
 * strong references to extra off-screen item views for caching purposes</p>
 */
public static abstract class ViewHolder {
 public final View itemView;

 .......

 public ViewHolder(View itemView) {
  if (itemView == null) {
   throw new IllegalArgumentException("itemView may not be null");
  }
  this.itemView = itemView;
 }

 .......
}

2. Let's see what we have done to inherit it.


public class XRViewHolder extends RecyclerView.ViewHolder {
 public XRViewHolder(View view) {
  super(view);
  ButterKnife.bind(this, view);
 }
 .......
 public void onBindViewHolder(VH holder, final int position) {
  if (position >= 0) {
   holder.itemView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     itemClick(getItem(position), position);
    }
   });
  }
 }
}

See that super (view) is also used in our custom construction method of XRViewHolder.

In fact, view here is the layout of item. In this case, it is very easy for us to realize click events, and we can directly use holder. itemView. setOnClickListener

Click Dislocation Problem of item

With XRecyclerView. getChildAt (position). setOnClickListener () dislocation problem, if you in addHeaderView will appear this dislocation problem will be more obvious, you can first look at XRecyclerView inside part of the source code.


public class XRecyclerView extends RecyclerView {

 private WrapAdapter mWrapAdapter;
 ......

 public void addHeaderView(View view) {
 sHeaderTypes.add(HEADER_INIT_INDEX + mHeaderViews.size());
 mHeaderViews.add(view);
 if (mWrapAdapter != null) {
  mWrapAdapter.notifyDataSetChanged();
 }
 }

 ......
 private class WrapAdapter extends RecyclerView.Adapter<ViewHolder> {

 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if (viewType == TYPE_REFRESH_HEADER) {
   return new SimpleViewHolder(mRefreshHeader);
  } else if (isHeaderType(viewType)) {
   return new SimpleViewHolder(getHeaderViewByType(viewType));
  } else if (viewType == TYPE_FOOTER) {
   return new SimpleViewHolder(mFootView);
  }
  return adapter.onCreateViewHolder(parent, viewType);
 }
}

Every time we addHeaderView, he will perform notifyDataSetChanged, and there is a corresponding isHeaderType judgment in onCreateViewHolder, so when you want to select the first item for event processing, it may be that pos=0 belongs to headerview.

Summarize

item Click Event: holder. itemView. setOnClickListener ()


Related articles: