RecyclerView OnClickListener (Best practice way)

28
14



This video is part of a course named “SQLIte for Beginners 2019”. It’s free and available on my website:

In this video I show you the best practice way to attach an OnClickListener to a RecyclerView and determine what list item was clicked.

➤Learn to code like me:

➤Source Code:


Nguồn:https://wijstaanvooronzegrondrechten.org/

28 COMMENTS

  1. Explaining what you code was doing as you typed, and at the end to summarized really helped!! Awesome tutorial!!

  2. Hello how you can implement a popup menu in the notelistener as u know the popupmenu needs a view so how i can set up? and another question is, what recomend a popup menu or a context menu? in my app i have to select in a menu to wich activity i want to go
    thanks for ur answer 🙂

  3. great video Mitch. thank you.

    there were like a gazillion comments (I know it's actually 202 :D) so I don't know if this has been asked before. I didn't get one thing. why each ViewHolder must have its own OnNoteListener? what if we did this instead:
    * pass the OnNoteListener to Adapter (and set it as its field)
    * ViewHolder implements the OnClickListener
    * in ViewHolder's onClick method, we call Adapter's OnNoteListener's onClick
    * pass 'this' to itemView's setOnClickListener method.

    Am I missing sth? Is sth wrong with this?

  4. Hey nice tips but whats the interface for ? Why not handle the click in the onClick method with getAdapterPostion if you don't need to change anything in the views ?

  5. Hi and thank you for all this amazing videos. I have a question. Can you tell me please how can I set an OnClickListener in a particular item with this method? Like a delete button or imageView… I'm coding only 2 month and it's a bit difficult for me to understand everything. Thank you

  6. Hi, great video! One question.
    In the adapter, when you add the click listener why is it needed to pass the OnNoteListener to the ViewHolder instead of using the one you have on the adapter directly? Is that bad practice? Can it cause problems?

  7. Do you have any tips for navigate from one fragment to another fragment?

    Some context so that you can beter understand my problem
    I am currently building a pokedex,
    I have fragment with a recyclerView where all the pokemons are shown.

    Now I want to make a fragment where you can see the details for a specific pokemon.
    But when i try your method i can't base the pokedex fragment to the adapter class.

    Do you have any tips or something i should look up?

  8. sir plez do this plez.
    @Override
    public void onClick(View v) {

    final Intent intent;
    switch (getAdapterPostion()){
    case 0:
    intent = new Intent(context, FirstActivity.class);
    break;

    case 1:
    intent = new Intent(context, SecondActivity.class);
    break;

    default:
    intent = new Intent(context, DefaultActivity.class);
    break;
    }
    context.startActivity(intent);
    }

  9. My itemclicklistener interface doesnt work when I change the dataset with notifydatasetchanged()?, also my notifydatasetchanged() doesnt work when i put everything on oncreateview in my fragment where I have my recyclerview, but my interface works. by what I mean by that is i am using buttons to categorize my items in recyclerview, but they don't work since on oncreateview i already have items query'd for all, but when i take that away and my buttons work, but my listener doesnt trigger when i click my items

  10. Thanks mitch, the other tutorial on another guy on how to setup a recyclerview had miss one thing on how to implement the listener to get clickevents, he forgot to add

    view.setonclicklistener(this);

    on the customviewholder. thanks again!

  11. This has really confused me. Am getting my data in the mainactivity from a ViewModel from room database.My problem is how to i pass it to the adapter.I also want to send that data to next activity using parcelable.

  12. I understand how you would intent but how would you intent if each recycler holder should intent to different things. I tried an if statement with posistion value but that didnt work for me

  13. hey mitch, can you give me some advice about parent-child recyclerview? example on parent-Recyclerview i have list country (USA) and when im click the item, passing to child-recyclerview list name of city (New York, LA, Las Vegas, etc)

  14. Hey Mitch, I was wondering if it's okay to implement onClickListeners inside onCreateViewHolder method of the adapter? I have implemented it and it works fine but I want to know if it's wrong in terms of performance and optimization?

  15. the video is really helpful, but it should be much easier than this. the same functionality in Swift/Objective-C is only a few lines of codes

LEAVE A REPLY

Please enter your comment!
Please enter your name here