Home » Android » Challenges in RecyclerView of Android L

Challenges in RecyclerView of Android L

Posted by: admin May 14, 2020 Leave a comment


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) {
        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
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;

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.

public int getItemCount() {
    return arrExperiences.size();

public void addExperience(String experience, int position){
    arrExperiences.add(position, experience);

public void removeExperience(){
    int index = (int) (Math.random() * arrExperiences.size());
How to&Answers:

Simply add this in your Adapter:

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


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.


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.


  • 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.


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