Click listener in RecyclerView

I had used RecyclerView several times and also handled click listeners. But my question is that in RecyclerView Adapter at which place setting the click listener is better, like in onCreateViewHolder() or onBindViewHolder() or in ViewHolder class. Because, I know at any place we set we can achieve our functionality.

Answers


You can set click listener for multiple views in your container view for various purposes. Best approach if it is an item click to add listener to container view :

private final OnClickListener mOnClickListener = new MyOnClickListener();

@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int position) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.containerViewItem, parent, false);
    view.setOnClickListener(mOnClickListener);
    return new MyViewHolder(view);
}

And implement onClick() method for your mOnClickListener .


The onBindViewHolder method is called every time when you bind your view with data.So the best place is to set click listener inside onCreateViewHolder method.

 private final OnClickListener mOnClickListener = new MyOnClickListener();

 @Override
  public ViewHolder onCreateViewHolder(final ViewGroup parent, final  int position) 
      {
       View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_list_item, parent, false);
       view.setOnClickListener(mOnClickListener);
       return new ViewHolder(view);
     }

 @Override
 public void onClick(final View view) {

      }

Need Your Help

How to access HTTP response url of websites using javascript

javascript python url response

Please edit the question if the title is not informative enough.

Laravel 5.1 Update form can't get it to work

forms laravel

I keep getting an : NotFoundHttpException in RouteCollection.php line 161: error whenever i try to update an user. I want it to work that an email gets filled in at zoekEmail input. And it will upd...