Item Click Listener - RecyclerView using Kotlin || kotlin Item Click Listener in RecyclerView

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video fox android so in this video we're gonna learn how to implement a item click listener inside a recycler view using kotlin as the backend language so in the last tutorial we have learned that how to implement a recyclerview so if you haven't watched that video already make sure you watch that video by clicking on this i button so let me just give you a brief demonstration of what you're going to get by the end of this video so if you can see on my mobile screen there are certain items inside the recycler view so if i click on any item for example if i click on the second item you can see you clicked on item 2 and 3 item 4 right so our cycler you can detect the item click listener and accordingly it is raising the tools so in this video i've just traced the toast on the item click listener you can manipulate it according to your application so if you want to learn the same make sure you watch this video till the end so without much to do let's get [Music] started so guys if you're into this channel make sure to hit that subscribe button and press the bell notification icon for saving the notification of the upcoming videos and by the end of the video if you like the video make sure to hit that thumbs up button as well so to implement this item click listener we gonna go to our adapter class that we have created in the last tutorial right so here we need to create an interface and i'll just name it as on item click listener right and inside this will declare a function on item click and it will take one argument that will be the position of the item on which user clicked so the type of which will be end right so we have declared this interface and now let us declare a variable the same interface that we have just created so i'll just name it as m listener right and the type of fetch will be on item click listener after that we need to create a function that is set on item click listener and now this function will take the object of this interface right so inside this we'll set this local m listener variable to the one that is passed in the arguments so let me do that so m listener is equal to listener so this particular one is the local variable that we have declared inside this class adapter class and this one will be passed as an argument so that is all we need to do here now we'll go to our myviewholder class so inside this will create a init method and before that we were taking only this itemview as an argument and we'll take another argument that will be let us name it as listener right and the type of which will be on item click listener now this if you're new to scotland in it you can consider this in it as the constructor that we make in java right so inside this will set the on click listener on the item view let me do that on item click listener and you need to select this one right with the curly braces now press enter listener dot on item click and we'll pass the position here wrapped up position right so this is what we need to do inside this myviewholder class now we need to change certain things inside this oncreateviewfolder so here we'll pass our m listener right so that is all we need to do inside this adapter class the very first thing is we created this interface we created a variable for the same we created this function and then inside this myviewholder class we have made the second argument that it'll take and we have written this code inside the constructor of this and the last thing we did is we passed this m listener here which is being returned by the uncreate view holder right now we'll go to our main activity where we have implemented the site view so now you can see we have set the adapter inside the cycler view so but for that now i'll create a reference variable that will point to our adapter let me name it as adapter only okay let me just cut this and paste it here right and we'll set the adapter to this recycler view now adapter dot set on item click listener all right now it will take the object of the interface that we have created my adapter dot on item click listener right for these curly braces now we have to implement the members of that interface so press alt enter by clicking on this object implement members now we need to implement this now here you will get the position on which the user clicked and now you can do whatever you want on the click event when the user clicked on any item of the recycler you write so for example i do not want to do anything fancy so i'll simply just raise a toast which will let us know that on which item user has clicked so for that i'll raise a toast so that is all we need to do to implement this on item click listener so let me quickly install this app to my cell phone and check if it is working fine or not right so okay guys the app has been installed to my cell phone as you can see on my mobile screen if i click on any of these items you can see the toast that you clicked on item number six so let me do that with the other items so you can see i guess we have successfully implemented this feature inside our android application right and i also hope that you guys have understood the code in case you have any doubt you can always ask me in the comment section or you can dm me on instagram the instagram username will be there on screen right now if you like the video make sure you hit that thumbs up button and in case you're new to this channel make sure to hit that subscribe button and press the bell notification icon for saving the notification of the upcoming videos so that is it for this video see another video bye [Music] [Music] you
Info
Channel: Foxandroid
Views: 7,743
Rating: 4.8967743 out of 5
Keywords: recyclerview onclicklistener, recyclerview onclicklistener to new activity, recyclerview onclicklistener interface, recyclerview onclicklistener kotlin, recyclerview onclicklistener open new activity, recyclerview onitemclicklistener, recyclerview onitemclicklistener in android studio, recyclerview onitemclicklistener in activity, recyclerview onitemclicklistener example, recyclerview onitemclicklistener kotlin, recyclerview onclick, adapterposition, 2021, kotlin, android, recyclerview
Id: dB9JOsVx-yY
Channel Id: undefined
Length: 7min 21sec (441 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.