RecyclerView OnClickListener (Best practice way)

Поделиться
HTML-код
  • Опубликовано: 27 ноя 2024
  • This video is part of a course named "SQLIte for Beginners 2019". It's free and available on my website: codingwithmitc...
    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:
    ↻ codingwithmitc...
    ➤Source Code:
    ↻ github.com/mit...

Комментарии • 374

  • @Richie_
    @Richie_ 5 лет назад +14

    Coding with Mitch & Coding in Flow are the 2 best tutors on RUclips.

  • @ianrknowles
    @ianrknowles 3 года назад

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

  • @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 5 лет назад +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!!!

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

  • @dungenhero7159
    @dungenhero7159 4 года назад

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

  • @hugocc0318
    @hugocc0318 Год назад

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

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

  • @josepenasanchez1491
    @josepenasanchez1491 4 года назад +2

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

  • @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!

  • @ericsmith2405
    @ericsmith2405 5 лет назад +1

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

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

  • @huh_wtf
    @huh_wtf 4 года назад +4

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

  • @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 🙌

  • @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 4 года назад

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

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

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

  • @apartmentbluerose-trogir7869
    @apartmentbluerose-trogir7869 4 года назад +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.

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

  • @dignitymashoko5437
    @dignitymashoko5437 5 лет назад +1

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

  • @kristians1550
    @kristians1550 2 года назад

    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

  • @codinginflow
    @codinginflow 5 лет назад +217

    Wow you must be a programmer or something

    • @codingwithmitch
      @codingwithmitch  5 лет назад +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 ;)

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

  • @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!

  • @prithvib8662
    @prithvib8662 4 года назад +1

    Your voice makes this sound like casually explained: programming

  • @demoscena1180
    @demoscena1180 5 лет назад +8

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

  • @nikhilcsuvarna8829
    @nikhilcsuvarna8829 3 года назад

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

  • @ki11qwerty47
    @ki11qwerty47 5 лет назад +10

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

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

  • @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!

  • @samoramabuya6583
    @samoramabuya6583 3 года назад

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

  • @madhuthakur472
    @madhuthakur472 5 лет назад +1

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

  • @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!

  • @sitibalqis8032
    @sitibalqis8032 5 лет назад +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 5 лет назад

      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 😂

  • @rishankyashu1121
    @rishankyashu1121 4 года назад

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

  • @BaldPL
    @BaldPL 4 года назад

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

  • @pikachubolt2001
    @pikachubolt2001 4 года назад +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!

  • @samha1513
    @samha1513 5 лет назад +1

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

  • @josephridge3646
    @josephridge3646 3 года назад +1

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

  • @nawazkamboo8750
    @nawazkamboo8750 2 года назад

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

  • @sonuchristopherk2273
    @sonuchristopherk2273 5 лет назад

    amazingly short and easy to understand.. Keep rocking!

  • @darshans9186
    @darshans9186 5 лет назад +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  5 лет назад +1

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

  • @aslammamud6078
    @aslammamud6078 3 года назад

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

  • @isurubandara899
    @isurubandara899 4 года назад

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

  • @martinsalcedoapps
    @martinsalcedoapps 4 года назад

    Really simple, great explanation. Thanks Mitch.

  • @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! >ㅁ

  • @vuphong2003
    @vuphong2003 5 лет назад +2

    Clearly explained, plus you made it so easy.
    Thanks!

  • @themax734
    @themax734 4 года назад

    Thanks Mitch! This was exactly what I needed.

  • @ryujikomai8859
    @ryujikomai8859 4 года назад

    Super helpful and straightforward. Thanks so much :)

  • @tanzimislam2395
    @tanzimislam2395 4 года назад +1

    Ty ma frnd.
    You have earned yourself a subscriber

  • @abdulazizfahim2140
    @abdulazizfahim2140 4 года назад

    Dude you are a LIFESAVER!!

  • @constanzacirion8100
    @constanzacirion8100 4 года назад

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

  • @patrickgichuki1789
    @patrickgichuki1789 4 года назад

    Life and time saver! Thank you very much!

  • @KapilTapsi
    @KapilTapsi 4 года назад

    Superb. Thank you sir. Very good tutorial

  • @mengmengliu6261
    @mengmengliu6261 4 года назад

    simple and elegant, thank you so much!

  • @kgi259
    @kgi259 2 года назад

    It's worked to my project! Thanks a lot

  • @HarshSharma-vd1rx
    @HarshSharma-vd1rx 4 года назад +1

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

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

  • @casenovo
    @casenovo 4 года назад

    Awesome Work Dude.
    Really Appreciate it.

  • @jhonesns
    @jhonesns 3 года назад

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

  • @nonamenoname3144
    @nonamenoname3144 3 года назад

    Thank you very much, you make me understand interfaces

  • @andre0667
    @andre0667 3 года назад

    Straight to the point!

  • @danylfernandes5903
    @danylfernandes5903 5 лет назад +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.

  • @Flight365days
    @Flight365days 5 лет назад +3

    Thanks. Great tutorial as usual.

  • @andrewfielden284
    @andrewfielden284 4 года назад

    Nice video. Very well explained.

  • @marenlampprecht6947
    @marenlampprecht6947 4 года назад

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

  • @javrz91
    @javrz91 4 года назад

    Excellent! This really helped a lot! Thanks!

  • @azammukhtar9011
    @azammukhtar9011 5 лет назад +4

    Lol, i just wondering how to do that tommorow, very nice thank you !

    • @iAdrianT
      @iAdrianT 5 лет назад +2

      Literally same LMAO

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

  • @vahidhoseini1723
    @vahidhoseini1723 4 года назад

    great, good job mitch.

  • @karagezify
    @karagezify 5 лет назад

    Thanks for making this! Really helped me out!

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

  • @andreslista4079
    @andreslista4079 5 лет назад +2

    Sir, great video. Thank you. Greetings from Venezuela!

  • @hectorfabianvignolo9960
    @hectorfabianvignolo9960 4 года назад

    Mil gracias desde Argentina!!!

  • @switchend7776
    @switchend7776 5 лет назад +1

    quality teaching bro thank you! learned a lot

  • @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!

  • @milleniumtv908
    @milleniumtv908 4 года назад

    This helped me a lots! Thanks very much

  • @sergeykuzmin8566
    @sergeykuzmin8566 4 года назад

    Thank you so much. It helped me with my project

  • @LiubomyrFatsiievych
    @LiubomyrFatsiievych 2 года назад

    Great tutorial, thank u so much!

  • @MrGedanggoreng
    @MrGedanggoreng 3 года назад

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

  • @DrHoffVan
    @DrHoffVan 5 лет назад

    thanks for the new and optimal way thanks a lot

  • @zadick2026
    @zadick2026 5 лет назад +2

    Thank you so much bro it saves my time

  • @hocineguebbas4016
    @hocineguebbas4016 4 года назад

    Thank you so match . you're the best ever.

  • @farnadtohidkhah1684
    @farnadtohidkhah1684 3 года назад

    Thank you very much for your decent video

  • @CalvinJKu
    @CalvinJKu 3 года назад

    Thanks Mitch. I'm using a similar pattern but I'd like to 1. Add an overlay when an item in the RecyclerView is selected; 2. Change some UI in the hosting fragment when something inside the RecyclerView is selected. I find it quite difficult as the onClick methods and the UI is controlled by the fragment, while the item UI is controlled by the viewholder. Not sure how to get them to talk to each other.

  • @vitalykrivolapov3163
    @vitalykrivolapov3163 4 года назад

    Thank you very much! Very helped.

  • @red2fire2
    @red2fire2 5 лет назад +16

    Cheers,
    This really helped

  • @aleksandrbut9604
    @aleksandrbut9604 5 лет назад

    thanks a lot, this is exactly what i was looking for

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

  • @enriqueajin6016
    @enriqueajin6016 3 года назад

    Helped a lot. Thank you :)

  • @codingwithshafi2019
    @codingwithshafi2019 5 лет назад +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'

  • @ROMAINPC
    @ROMAINPC 4 года назад

    You saved my life

  • @piermarcoroccato5203
    @piermarcoroccato5203 4 года назад +1

    Tnk very helpful for me

  • @hoaphung6604
    @hoaphung6604 3 года назад

    Great video tutorial. Question!!!! How do I disable certain clicking positions ? For instance I don’t want note 5 to be clickable.

    • @amalsunil4722
      @amalsunil4722 3 года назад

      In that case you could try using the "position" value to find the ViewHolder you want to make unclickable and do something like setClickable(false)

  • @nealratan6917
    @nealratan6917 4 года назад +1

    This the aptest way I have found. Other implementation have got me bugs like onclick getting called twice.
    But still I am struggling to add onClickListener to the child of ItemView.

  • @alpertemell
    @alpertemell 4 года назад

    thanks, i implemented it to my code easily.

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

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