12
votes

I have been playing around with RecyclerView for a little bit. Is there any easy way to put OnClickListener for items in RecyclerView? I have tried implementing it in ViewHolder. The onClick event never got triggered.

And I have used notifyItemInserted(position) for adding new value into RecyclerView. The UI does not got refreshed automatically. Needed to pull up and down to refresh. But when I invoke notifyDatasetChanged(..), it is ok.

I have applied DefaultItemAnimator to RecyclerView. But, not seeing any animation when new item added.

Thanks advance for any idea.

This is the first Android L component I have tested out and I am stucking there.

Here is my Adapter class:

public class AdapterRecyclerView extends RecyclerView.Adapter<AdapterRecyclerView.MyViewHolder> {

private List<String> arrExperiences;

//Provide a reference to the type of views that you are using - Custom ViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView tvExperienceTitle;
    public TextView tvExperienceDesc;

    public MyViewHolder(RelativeLayout itemView) {
        super(itemView);
        tvExperienceTitle = (TextView) itemView.findViewById(R.id.tv_experience_title);
        tvExperienceDesc = (TextView) itemView.findViewById(R.id.tv_experience_desc);
    }

}

//Provide a suitable constructor : depending on the kind of dataset.
public AdapterRecyclerView(List<String> arrExperiences){
    this.arrExperiences = arrExperiences;
}

//Create new view : invoke by a Layout Manager
@Override
public AdapterRecyclerView.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RelativeLayout view = (RelativeLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item_recycler, parent, false);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(AdapterRecyclerView.MyViewHolder viewHolder, int position) {
    //get element from your dataset at this position.
    //replace the content of the view with this element.
    viewHolder.tvExperienceTitle.setText(arrExperiences.get(position));
}

@Override
public int getItemCount() {
    return arrExperiences.size();
}

public void addExperience(String experience, int position){
    arrExperiences.add(position, experience);
    notifyItemInserted(position);
    //notifyDataSetChanged();
}

public void removeExperience(){
    int index = (int) (Math.random() * arrExperiences.size());
    arrExperiences.remove(index);
    notifyItemRemoved(index);
    //notifyDataSetChanged();
}
}
4
This is exactly I said above. That guy is implementing OnClickListener from ViewHolder. I tried it. But, onClick event never got triggered.Aung Pyae
His code calls setOnClickListener(). Yours shown above does not.CommonsWare
How do you managed pull to refresh in RecyclerView?Pramod

4 Answers

2
votes

Simply add this in your Adapter:

@Override
public void onBindViewHolder(AdapterRecyclerView.MyViewHolder viewHolder, int position) {
    yourItems.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          //do your stuff
       }
   });
}
0
votes

Please see my answer here. You do need an extra class (which may be included as part of the full release) but it will allow you to create OnItemClickListeners the way you are used to for ListViews.

0
votes

Since you still didn't mark correct any answer, and even if it's an old question, I will try to provide the way I do. I think it is very clean and professional. The functionalities are taken from different blogs (I still have to mention them in the page), merged and methods have been improved for speed and scalability, for all activities that use a RecycleView.

https://github.com/davideas/FlexibleAdapter

  • At lower class there is SelectableAdapter that provides selection functionalities and it's able to maintain the state after the rotation, you just need to call the onSave/onRestore methods from the activity.
  • Then the class FlexibleAdapter handles the content with the support of the animation (calling notify only for the position. Note: you still need to set your animation to the RecyclerView when you create it the activity).
  • Then you need to extend over again this class. Here you add and implements methods as you wish for your own ViewHolder and your Domain/Model class (data holder). Note: I have provided an example which does not compile because you need to change the classes with the ones you have in your project.

I think that, it's the ViewHolder that should keep the listeners of the clicks and that it should be done at the creation and not in the Binding method (that is called at each invalidate from notify...() ).

Also note that this adapter handles the basic clicks: single and long clicks, if you need a double tap you need to use the way Jabob Tabak does in its answer.

I still have to improve it, so keep an eye on it. I also want to add some new functionalities like the Undo.

0
votes

Here you get a simple Adapter class which can perform onItemClick event on each list row for the recyclerview.