RecyclerView Item Click | Best Practice Way

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everyone in this video what i'm going to do is show you how to correctly add on click listeners to each of the items within your recycler view this should be super easy to do so let's get started in the last video we set up this recycler view here right pretty simple the most basic recycler view you could ever have if we click on any of these items nothing happens so you can hear me i'm clicking away on arginine when we get nothing now by the end of the video we should have our recyclerview responding to user clicks so in my case if i click on arginine we have a new activity loaded and we get some more information about the molecule if i go back we could come down let's click on let's say tryptophan this is a pretty good one we get some more information about the tryptophan molecule now how do we even get started with this the first thing we're going to do is create an interface so let's come over to the far left come down to this base most folder that's holding your main activity we're going to right click on it new java class like usual but this time make sure you select the interface option right here and then i'm going to go ahead and just call this recyclerview interface and then i'm going to go ahead and hit enter and then hit enter again and you should have something that looks very similar to this now what is an interface essentially it's a contract between any class that implements it and it's just making sure that that class has whatever methods are listed within our interface now what methods do we want to define within our interface and unfortunately the answer to that question is it depends on what you want to do in our case we want to respond to user clicks on each of the items within the recycler view so we should define a method within our interface that's going to be responsible for that to create this method we're going to type in void because we don't want it to return anything and we need to pick a name next for this method i'm going to call it on item click because that just makes the most sense but you can go ahead and call it whatever you want and then this method should also take in a position and that'll make more sense once we start writing some more code all right sweet we're about a third of the way done with this whole process now that we have our interface defined let's go over to the main activity or wherever your recycler view is created and we're going to scroll all the way up to the top and we're going to type in the following implements the recyclerview interface so i'm going to come down hit enter and we should get a little error message right here if you hover over it you get this little dialog box click on implement methods or you can come over to the red light bulb and click on implement methods there now we only have one method defined in our interface so make sure you have that one selected and then click ok and android studio should add in the following code for you then all the code that you want executed when the user clicks on an item should be placed within the on itemclick method we have here now before i go ahead and write all that mumbo jumbo what i want to do is go over to the recyclerview adapter class and write the code we need in there so let's come over to that class and scroll all the way up to the top and we need to define a variable to hold our recyclerview interface so i'm going to type in private final the recyclerview interface i'm just going to call it recyclerview interface and then we need to add this to our constructor so we're going to add a constructor parameter of type recyclerview interface just going to call it recyclerview interface to keep things simple and then we're going to do the usual this dot recyclerview interface equals the interface that was passed in so recyclerview interface so now that we have this variable populated with whatever's passed into our constructor we're going to come down to the constructor of our inner class then within here what we need to do is attach an on click listener to our item view to do this pretty simple we type in itemview.setonclicklistener is going to equal the new view.onclicklistener and within here what we need to do is perform a few checks the first check we should do is make sure that the recyclerview interface isn't null so we could type in if the recyclerview interface does not equal null now looking at this you can see that we have an error right here this is because our inner class is static we don't have access to this variable here now one way to get around this error that we have is to make this class non-static so if we come back down you can see the error message is gone but this isn't recommended make sure you keep this class static the way we get around this is by coming up to the constructor of our my view holder and passing in the recyclerview interface so we could type in the recyclerview interface cyclerview interface and if we come down to the on click you can see the air is gone now that we know that our recyclerview interface isn't null we need to grab the position from our adapter because if we come back to the interface that is needed for our on click method so let's come back over we can grab that position by typing in int position set that equal to git adapter position and then we can come down here and we need to make sure that this position is valid so we can type in position does not equal the recyclerview dot no position and then if this is valid we want to call this method we have defined within our interface so we type in the recyclerview interface dot on item click and we're going to pass in the position that we extracted from our adapter so that is everything you need in the on click listener now there's a few things we need to fix within our adapter class since we created this extra constructor parameter we need to come up to the oncreateviewholder and pass in this recyclerview interface so we're going to go comma recyclerview interface and then that should fix all the errors within our recyclerview adapter class but if we go back over to the main activity if we scroll up you can see we have an error when we're setting up our adapter that's because we also added in an extra parameter to our constructor the recyclerview interface so going back to the main activity since we implemented the recyclerview interface all we have to do is pass in this and the error should be gone so that should be everything you need to respond to user clicks for your recyclerview items the very last thing i have to do is write all the code to swap over to a new activity when an item is clicked i'm going to go ahead and just cut to that so you don't have to watch me do it if you want to know how to swap to a new activity be sure to check the video i posted on how to do that but i don't think any of you want to watch me write all that code so i'll be back okay so i wrote all the code that we needed for the on item click to swap over to a new activity so i just want to explain what i did here all i did was set up a new intent to swap over from the main activity to main activity 2. i extracted the information from our amino acid model based on whatever position was given to us by the adapter now this isn't the best way to go about doing it you should create a parcel here and pass that over to the main activity too but for the sake of this video i just did it this way because it's so much easier so then we just start up that activity come over to mainactivity2 then in the main activity i just extracted all the variables that i passed over and again use a parcel to pass over that object so your code doesn't look like this then i grabbed all the text views and image views stored them within variables so all these guys here from the main activity2.xml layout i grabbed all these views that i set up in here stored them within the java code and then i just assigned their values based on what i extracted from the intent and i did change up the amino acid model class really quick i added a description to be stored within our model class added a getter added something to the constructor and that's about it so let's come over to the far right click the play button to make sure that i did everything right so here's our recycler view video 13. if i scroll down here let's click on proline if we click it a new activity loads up we have our image of the molecule and a little description i can go back and click on any one of these and we get more information about the molecule so that's all i have for you guys if you have any questions about what i did at any point during this video including the sped up portion you can always leave a comment down below or message me on discord link to the server down in the description don't forget to like and subscribe thanks for watching and i'll see you guys in the next video
Info
Channel: Practical Coding
Views: 89,478
Rating: undefined out of 5
Keywords: RecyclerView Item Click Listener, recyclerview onclicklistener, recyclerview android studio, recyclerview android, Item Click, recycler view, recyclerView interface, interface, RecyclerView item click, recyclerview using interface, onclicklistener, onitemclick, recyclerview onclick, RecyclerView, RecyclerView java, android studio, recyclerview, recyclerview basic example, recyclerview example, android studio basics, tutorial, recyclerview java, android studio tutorial
Id: 7GPUpvcU1FE
Channel Id: undefined
Length: 8min 6sec (486 seconds)
Published: Fri Oct 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.