RecyclerView OnClickListener (Best practice way)

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

ความคิดเห็น • 374

  • @Richie_
    @Richie_ 5 ปีที่แล้ว +14

    Coding with Mitch & Coding in Flow are the 2 best tutors on TH-cam.

  • @joeypeters2916
    @joeypeters2916 4 ปีที่แล้ว +14

    Mitch, I have a degree in mobile app development. I wish I would have seen this while in school. It helped a lot and will continue to help a bunch. Thank you for all you do.

    • @noodler911
      @noodler911 3 ปีที่แล้ว

      5:36 the this keyword doesn't work for me. It provides ValueEventListener instead of OnNoteListener. Do you know how I can solve this problem?

  • @milkjpg
    @milkjpg 6 ปีที่แล้ว +6

    i have a project due in 3 days and your channel is saving my life!! thank you so much for this!!! never clicked on subscribe this fast!!!

  • @harisdurrani8422
    @harisdurrani8422 4 ปีที่แล้ว

    Superb...i searched every where but all were directing to onBind Holder...and that was very confusing for me,,, but your method is very very easy...and it solved all my problem...love you for this helpful guide....

  • @codinginflow
    @codinginflow 6 ปีที่แล้ว +217

    Wow you must be a programmer or something

    • @codingwithmitch
      @codingwithmitch  6 ปีที่แล้ว +43

      😏

    • @goldwings1468
      @goldwings1468 5 ปีที่แล้ว +10

      Coding in Flow approves 😃

    • @anhchung7340
      @anhchung7340 5 ปีที่แล้ว +3

      funny :)

    • @ehtashamali821
      @ehtashamali821 5 ปีที่แล้ว +2

      Ohh Glad to see you here. You both are Best.
      Please guide me on this issue. I try to resolve this issue from last two weeks but not getting solution yet.
      How can I change the View which is previously clicked in RecyclerView.
      I have a play button in every single row of RecyclerView. Whenever this button is clicked button visibility changed like shown in this image imgur.com/0fpk0Jv.
      When user click on first row button visibility changed which is working fine. But when user click on second row button of RecyclerView I want to change the button of first row to came back to its original position. Scenario shown in this image imgur.com/n4Q26Qm

    • @tarikbenaliamar4033
      @tarikbenaliamar4033 5 ปีที่แล้ว +1

      Codingin Flow you too, you are a genius ;)

  • @hugocc0318
    @hugocc0318 2 ปีที่แล้ว

    Omg I have learned more in this video than searching through a thousand forums, thank you very much

  • @ianrknowles
    @ianrknowles 3 ปีที่แล้ว

    Tried to get a recyclerview click listener working for hours - this tutorial finally cracked it - Thank you!

  • @WftYT
    @WftYT 2 ปีที่แล้ว

    Thank you so much. I've got a huge project rn and the only thing that was troubling me is communication between the recycler view and the Activity that uses it. Thank you so much! It fixed my issue

  • @klekaelly
    @klekaelly 3 ปีที่แล้ว +1

    Hey Mitch, just wanted to say I was stuck on a problem for a long time and your video (especially the part where you took us back and walked through everything we did) was super helpful. I was able to resolve my problem after re-approaching it with a clear head. Thanks for the guidance!

  • @jarrodthoma1866
    @jarrodthoma1866 4 ปีที่แล้ว +2

    Mitch, I definitely appreciate your content. Software/firmware development is nothing new to me, but I recently got into app development, and this series has helped tremendously! I was having difficulty with changing the TextView text color within my ViewHolders in the RecyclerView, but was eventually able to figure it out with your interface examples on the Note RecyclerView series. Thank you so much.

  • @josepenasanchez1491
    @josepenasanchez1491 4 ปีที่แล้ว +2

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

  • @moazelsawaf2000
    @moazelsawaf2000 5 ปีที่แล้ว +8

    Thanks a lot ❤️
    This method is called inter fragment communication and we use it when we want our fragment to communicate with the activity...

  • @kristians1550
    @kristians1550 3 ปีที่แล้ว

    Had to come back to this video to say how much I appreciate what you do,
    I have used this in my recyclerView app a long time ago and now I was looking for a way to fix a similar issue in different java swing application where I used the same concept with an interface and it works as a charm. Man you really have no competition when it comes to this stuff.
    Wish you pleasant holidays mate, you deserve it

  • @dungenhero7159
    @dungenhero7159 4 ปีที่แล้ว

    I went to StackOverflow, couldn't find the solution. Then I found this. Worked like a charm. Thanks a lot

  • @marcoantonioblancastokunag1864
    @marcoantonioblancastokunag1864 3 ปีที่แล้ว

    I am very happy to find your content. Excellent explanation, I have signed up for the full course on your website. Thank you!

  • @nikhilcsuvarna8829
    @nikhilcsuvarna8829 3 ปีที่แล้ว

    Thank you for this..I was searching whole day for this..🙌

  • @apartmentbluerose-trogir7869
    @apartmentbluerose-trogir7869 5 ปีที่แล้ว +14

    God bless you Mitch! It works like a charm!

  • @Tronx431
    @Tronx431 3 ปีที่แล้ว

    2021 ik but thx alot dude, you fixed my crahing issues. I have my holder class separated from my adapter class so every time I clicked one of my items it crashed but not anymore, ur the best.

  • @huh_wtf
    @huh_wtf 5 ปีที่แล้ว +4

    This was such a god sent....Thank you for uploading this. Such an elegant solution.

  • @mikolajm9175
    @mikolajm9175 4 ปีที่แล้ว

    This is gold among so much shitty stack answers i found. Much thanks for this explanation and BRILLIANT revision at the end.

  • @chloegeldhof4921
    @chloegeldhof4921 5 ปีที่แล้ว +8

    Thank YOU!!!! I changed it a little to apply to a fragment but after hours of finding shite tutorials it's finally working! I'll put your name on my bachelors degree LOL

    • @wertin200
      @wertin200 5 ปีที่แล้ว

      Did you find a way to
      Go from one fragment to
      Another fragment
      And then to show the id that was passed through

  • @GauthamKrishna07
    @GauthamKrishna07 5 ปีที่แล้ว +3

    Hey Mitch. I have an error at 5:41. I already passed getApplicationContext and some other variables in the initialisation of Recycler adapter.

  • @josephridge3646
    @josephridge3646 4 ปีที่แล้ว +1

    Hey Mitch thank your for this !!!
    I appreciate the relay.

  • @sitibalqis8032
    @sitibalqis8032 6 ปีที่แล้ว +1

    Thank you Mitch! I finally be able to go to another different activity for each item clicked! Implemented the switch case in the onNoteClick and it worked! :)))) p/s: I used the coding from your previous recyclerview and edited from there.

    • @sitibalqis8032
      @sitibalqis8032 6 ปีที่แล้ว

      But only for the toast message, i don't know where to put the coding. But i guess it's okay as long as my goal is achieved 😂

  • @ericsmith2405
    @ericsmith2405 5 ปีที่แล้ว +1

    Just fantastic! Finally someone who can break down a myriad of core topics into a one simple brain folder!!

  • @nawazkamboo8750
    @nawazkamboo8750 2 ปีที่แล้ว

    you're awesome dude. Really it is the Best Practice Way!!
    My Issue was solved

  • @madhuthakur472
    @madhuthakur472 6 ปีที่แล้ว +1

    Really appreciate the way you explain things .. made android easy for me ..

  • @BaldPL
    @BaldPL 4 ปีที่แล้ว

    Thanks for help! Good quality example of create own listener and use it in RecyclerView.

  • @dignitymashoko5437
    @dignitymashoko5437 6 ปีที่แล้ว +1

    Man you are the best, keep up you are saving the world.

  • @pikachubolt2001
    @pikachubolt2001 5 ปีที่แล้ว +1

    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!

  • @samusaran5509
    @samusaran5509 5 ปีที่แล้ว

    Man i have been racking my brain trying to figure out why my click listener wasn't passing variables to a new activity. Thanks so much my dude.

  • @John2025-Crown
    @John2025-Crown 4 ปีที่แล้ว

    Hi, first I'd like to thank you for your videos and lessons, and then I got two questions that I hope you have time to answer.
    1. How to make each item to open a new activity (not the same) example item (A) opens activity (A) and item (B) opens activity (B) and so on.
    2. How to implement onBackPressed() or finish() method in the clickListener method of recyclerView, for example if item (A) clicked open activity (A) and finish its current activity.
    Thank you again.

  • @aslammamud6078
    @aslammamud6078 3 ปีที่แล้ว

    Your tutorial helped me a lot. thanks. all the best.

  • @matthewmirandaa
    @matthewmirandaa 3 ปีที่แล้ว

    I know the video's a couple of years old but I just really need to say that this was a great video/explanation, thank you so much, helped out a lot, pura vida 🙌

  • @darshans9186
    @darshans9186 6 ปีที่แล้ว +1

    Oh shitt!! Have to make lots of changes in the code which I wrote today 😅 thanks alot mate. It's really helpful. Keep posting good stuff

    • @codingwithmitch
      @codingwithmitch  6 ปีที่แล้ว +1

      I think next month I'm going to start posting shitty stuff

  • @sonuchristopherk2273
    @sonuchristopherk2273 5 ปีที่แล้ว

    amazingly short and easy to understand.. Keep rocking!

  • @kneedeep185
    @kneedeep185 5 ปีที่แล้ว +1

    I learned multiple new things today, this first being that you can actually implement multiple interfaces in a class declaration. I couldn't figure out how to get this to work (I'm using a cursor and SQLite db), but it turns out you can implement multiple interfaces by separating with commas.
    Class Xyz implements Manager.Cursor, Adapter.Listener {
    Ha! Thanks for the video, super helpful!

    • @codingwithmitch
      @codingwithmitch  5 ปีที่แล้ว +1

      On another note, you should try using the room persistence library instead of the old "cursor" way with SQlite. I have a free course if you're interested. codingwithmitch.com/courses/sqlite-room-persistence-android/

    • @kneedeep185
      @kneedeep185 5 ปีที่แล้ว

      @@codingwithmitch Thanks yeah I'll definitely try that out. It seems like a constant struggle finding what's been deprecated and what is still like 5 years old but still heavily used. I realized a little late into my project that (apparently) the Cursor is dead.

    • @codingwithmitch
      @codingwithmitch  5 ปีที่แล้ว

      @@kneedeep185 that's why you gotta follow me. I do all the hard work for you

  • @vladkondratiuk5886
    @vladkondratiuk5886 5 ปีที่แล้ว +5

    That's great, dude! When I was finding on Russian chanels this info, I didn't do that, but when I saw your video, I understand that no metter what I need, I have to search everything on English chanels at ones!

  • @chriscross12324
    @chriscross12324 4 ปีที่แล้ว

    Finally helped me get my code up and running, migrating everything from Java to Kotlin was pretty seamless but the Adapter definitely gave me a couple migraines. Thanks!

  • @noodler911
    @noodler911 3 ปีที่แล้ว

    5:36 the this keyword doesn't work for me. It provides ValueEventListener instead of OnNoteListener. Do you know how I can solve this problem?

  • @rishankyashu1121
    @rishankyashu1121 4 ปีที่แล้ว

    wow..... nice explained iwas stuck on this problem for more than a day.....

  • @martinsalcedoapps
    @martinsalcedoapps 4 ปีที่แล้ว

    Really simple, great explanation. Thanks Mitch.

  • @samoramabuya6583
    @samoramabuya6583 4 ปีที่แล้ว

    Listen to me now. Thanks bro. This helped me with a problem I was having.

  • @isurubandara899
    @isurubandara899 4 ปีที่แล้ว

    Thank you man, I have been using the wrong one that you mentioned. I did not know that Thank you again.

  • @Kevin-HB
    @Kevin-HB 3 ปีที่แล้ว

    Man, you help my life. THANK YOU SO MUCH!!!!!!

  • @보바-q4k
    @보바-q4k 5 ปีที่แล้ว +1

    Thank you sooooooooo much for your super kind explanation!! I found it so easy to follow. I've actually tried several ways to apply OnClickListener to recycler items but I had failed to come here! I'll keep subscribing for additional help! >ㅁ

  • @ki11qwerty47
    @ki11qwerty47 5 ปีที่แล้ว +10

    omg!!! thanks a lot! i killed 1day to find best practice way for this )))

  • @themax734
    @themax734 4 ปีที่แล้ว

    Thanks Mitch! This was exactly what I needed.

  • @randy4443
    @randy4443 4 ปีที่แล้ว

    Hi would you handle items inside the recyclerview click listener. For example, lets say there is a checkbox inside the recyclerview and you want to handle the click event .

  • @demoscena1180
    @demoscena1180 6 ปีที่แล้ว +8

    Interesting. I was always using the "bad practice" way.

  • @RiccardoGabellone
    @RiccardoGabellone 4 ปีที่แล้ว

    Hi!
    What's the best practice to do the same in Kotlin?
    I'm trying to solve an issue in my app with a RecyclerView in a fragment with the Search filtering and conditional clicking (if the item has a field equals "something", its click navigate to another fragment, else show a Snackbar)...I put the OnClickListeners inside the OnBindViewHolder and everything works fine till I search for a specific item in list: if I filter the results obtaining a "navigationable" item, if I click on it all it's ok; if I continue the search, filtering by the one with Snackbar, and I click on it, all it's ok. But If I reset my SearchView, making all the entire list appear again, and I click on the first item, that allowed me the navigation at the beginning, it will show me the Snackbar with the message belonging to the last item that I previously filtered that didn't allow me the navigation. It seems to me that the last OnClickListener, obtained in the search, overrides the previous one and bbrakes the algorithm functionality, or something like this. Hope I explained more or less my issue. THANK YOU IN ADVANCE!

  • @musicinsession
    @musicinsession 4 ปีที่แล้ว

    Thanks for this mate, but why is it that I get -1 or no_position from getAdapterPosition sometimes when I delete things from the list?

  • @tanzimislam2395
    @tanzimislam2395 4 ปีที่แล้ว +1

    Ty ma frnd.
    You have earned yourself a subscriber

  • @thebluer4829
    @thebluer4829 6 ปีที่แล้ว +1

    Can i ask sir? how about when i try using firebase onclick listener on adapter. how can i make it like that? because when i try to make constructor i got an error that i should use ValueEvenListener than OnClickListener and i got an error too when i use the getAdapterPositions null pointer

  • @ryujikomai8859
    @ryujikomai8859 4 ปีที่แล้ว

    Super helpful and straightforward. Thanks so much :)

  • @pootispow3017
    @pootispow3017 5 ปีที่แล้ว

    Question. Now how can we do this in an application that uses MVVM architecture? and instead of an activity, lets use a fragment and initialize the recylerView and the adapter inside the Fragment?

  • @KapilTapsi
    @KapilTapsi 4 ปีที่แล้ว

    Superb. Thank you sir. Very good tutorial

  • @MrGedanggoreng
    @MrGedanggoreng 3 ปีที่แล้ว

    Hi. Can you help explain how to implement OnClickListener to open url? I've searched everywhere but no solution found. Thanks.

  • @casenovo
    @casenovo 4 ปีที่แล้ว

    Awesome Work Dude.
    Really Appreciate it.

  • @it-series-music
    @it-series-music 4 ปีที่แล้ว

    @CodingWithMitch
    You have just mentioned performance issues but please give a detailed explanation of performance issues in the old method and how this method solves it ?.

  • @abdulazizfahim2140
    @abdulazizfahim2140 4 ปีที่แล้ว

    Dude you are a LIFESAVER!!

  • @andre0667
    @andre0667 3 ปีที่แล้ว

    Straight to the point!

  • @Asma-od1vf
    @Asma-od1vf 3 ปีที่แล้ว

    Hello
    I have question , if I want to add switch button in recyclerview item to disable or enable the item from click ,how can I do that please?

  • @RealAndroidTurorials
    @RealAndroidTurorials 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 ?

  • @patrickgichuki1789
    @patrickgichuki1789 4 ปีที่แล้ว

    Life and time saver! Thank you very much!

  • @jhonesns
    @jhonesns 3 ปีที่แล้ว

    Thanks, it worked for me, I'm really happy, thank you so much!

  • @HarshSharma-vd1rx
    @HarshSharma-vd1rx 4 ปีที่แล้ว +1

    please make a video on passing object as an intent. Great video btw;)

  • @vahidhoseini1723
    @vahidhoseini1723 4 ปีที่แล้ว

    great, good job mitch.

  • @kgi259
    @kgi259 2 ปีที่แล้ว

    It's worked to my project! Thanks a lot

  • @adamhemeon734
    @adamhemeon734 4 ปีที่แล้ว

    Is there a Kotlin version of this video? I followed the Kotlin version of the making a Recycler View you made.

  • @danylfernandes5903
    @danylfernandes5903 6 ปีที่แล้ว +5

    Hey Mitch! Would you make a short video on how to make an expandable recyclerview.
    Each list item expands to show sub list items
    Thanks.

    • @GauravLonkar
      @GauravLonkar 5 ปีที่แล้ว +1

      Yes, please. We need that.

    • @enzowalker2118
      @enzowalker2118 5 ปีที่แล้ว

      Look into Paging Library for Android Studio. Maybe that might be what you're referring to.

  • @mengmengliu6261
    @mengmengliu6261 4 ปีที่แล้ว

    simple and elegant, thank you so much!

  • @JoJo-yu7hh
    @JoJo-yu7hh 4 ปีที่แล้ว +1

    I can't do it I did everything as in the video it only works once when starting, after which it stops responding. It's probably because of the OnClickListener. Can it be because of the CardView that is in a different layout?

  • @mironoff2007
    @mironoff2007 3 ปีที่แล้ว

    Activity is passed by (this) to adapter to create listener. So adapter has activity reference. But what's the difference to pass activity by reference and call method or field directly?

  • @coquirivva
    @coquirivva 4 ปีที่แล้ว

    Hi Mitch, what if you have more than one click in the recyclerview item, such as a button, beside the main item.

  • @samha1513
    @samha1513 6 ปีที่แล้ว +1

    Clear and to the point as always. Everytime I watch your video I learn something new. Thanks.

  • @constanzacirion8100
    @constanzacirion8100 4 ปีที่แล้ว

    this is exactly what i was looking for. thanks :)

  • @nonamenoname3144
    @nonamenoname3144 3 ปีที่แล้ว

    Thank you very much, you make me understand interfaces

  • @andrewfielden284
    @andrewfielden284 4 ปีที่แล้ว

    Nice video. Very well explained.

  • @hilmydzul5034
    @hilmydzul5034 5 ปีที่แล้ว

    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)

  • @pallavipipalwa3658
    @pallavipipalwa3658 5 ปีที่แล้ว +2

    the new activity is not opening on clicking a particular item once. I have to click it several random number of times after which the intent works. And this is happening with all the items.
    Please help

  • @codingwithshafi2019
    @codingwithshafi2019 6 ปีที่แล้ว +1

    when using recyleview in fragment, my constructor is itemByConveyanceIdAdapter = new ItemByConveyanceIdAdapter(context,items,this) , this getting wrong . suggestion to Change 3rd parameter of method 'ItemByConveyanceIdAdapter' from 'OnNoteListener' to 'Callback'

  • @Mfbzai
    @Mfbzai 2 ปีที่แล้ว

    But how do you implement multiple interface to the activity for any condition then?

  • @wertin200
    @wertin200 5 ปีที่แล้ว

    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?

  • @67Diadem
    @67Diadem 3 ปีที่แล้ว

    Great tutorial!! I have a question and I wanted to implement that on my project, how will you put a custom back button on new detail activity so that whenever a user press on custom back button then it goes back to recycler view list items. ..? Can you please help me with that?

  • @javrz91
    @javrz91 4 ปีที่แล้ว

    Excellent! This really helped a lot! Thanks!

  • @emngaiden
    @emngaiden 4 ปีที่แล้ว

    I have a question I hope is not too late. Making whole activities to implement a listener, is it good practice? is less expensive than creating a new class file for each specialized listener?

  • @Flight365days
    @Flight365days 5 ปีที่แล้ว +3

    Thanks. Great tutorial as usual.

  • @chriskavanagh2596
    @chriskavanagh2596 5 ปีที่แล้ว

    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

  • @pyccmischa
    @pyccmischa 2 ปีที่แล้ว

    Good explanation.
    I find it at times difficult to see, what good practices are with anroid components.
    The official documentation tends to give examples that are simple to understand, but are frequently adviced against.
    e.g. in the case of making api call on the main thread.

  • @chinzimike
    @chinzimike 5 ปีที่แล้ว

    Great thing. But if I get the callback to a fragment and try to get the activity (getActivity() ) I get a null pointer error)... so how can get this after the callback you did?
    Thx in advance

  • @rhupin5276
    @rhupin5276 2 ปีที่แล้ว

    I love this best practice video! By the way, does this also apply to the onClick method of onSwipe from ItemTouchHelper? I made a separate utility java files that extends ItemTouchHelper.SimpleCallBack. Should I also execute the commands inside this onClick, in the Activity class instead?
    Thanks so much in advance!

  • @lerouxvermeulen4403
    @lerouxvermeulen4403 3 ปีที่แล้ว

    Dude...I was stuck on filtering one recyclerview from another for 3 days, then I implemented my onclick listeners like this, and now it works! thank you so much for this. Where can we find this by the way, is this on the documentation? Just know that you have saved my sanity xD

  • @karagezify
    @karagezify 5 ปีที่แล้ว

    Thanks for making this! Really helped me out!

  • @YeacYT
    @YeacYT 5 ปีที่แล้ว +1

    is it necesarry that the view holder's class is inside the adapter?

    • @codingwithmitch
      @codingwithmitch  5 ปีที่แล้ว +1

      Nope you can have it as a separate class

  • @pikachubolt2001
    @pikachubolt2001 5 ปีที่แล้ว

    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

  • @marenlampprecht6947
    @marenlampprecht6947 4 ปีที่แล้ว

    Wow!!! This is a great tutorial! Thank you :)

  • @gigajoules6636
    @gigajoules6636 4 ปีที่แล้ว

    So I have my recyclerview positioned inside of a fragment and want to make it so any image I press inside of my recyclerview is highlighted / selected when i do so.
    How do I acheive this? I have the debug window which states "Image n" pressed correcly when I click on an image, I'm just a little lost as to how I can get a handle on the imageviews from here.

  • @prithvib8662
    @prithvib8662 4 ปีที่แล้ว +1

    Your voice makes this sound like casually explained: programming

  • @ocity5905
    @ocity5905 3 ปีที่แล้ว

    pls mine is saying an error there is no default constructor in the recycle View Adapter