‫Handling click events on recycler view items involves implementing a mechanism to detect when an item
‫in the list is clicked and then performing some action based on that click.
‫In this video, we'll follow a step by step guide on how to achieve handling click events.
‫Let's start with the first step by creating an interface called Item Click Listener.
‫We need to implement this item.
‫Click listener in the viewholder.
‫So this interface will act as a contract between the adapter and the activity or fragment that uses
‫the recycler view.
‫And we need to define within this interface a method to handle item clicks.
‫I start by onClick and I pass two parameters.
‫The first one is of type view and the second parameter is of type int.
‫This allows us to return a view object on the item clicked and also detect the position of the item
‫click.
‫The second step is to modify our view holder in the adapter class.
‫So I'll start by creating a public item.
‫Click Listener.
‫Click listener and I need to link it with view dot item click Listener.
‫So for that I'll start by creating a method that allows us to to link between this click listener in
‫the main activity and assign it to the view on Click Listener interface Set on Click Listener.
‫I need to pass one parameter of type item.
‫Click Listener.
‫My listener creating a link between the click listener and the view dot Click listener Later on.
‫When we use this method, I need to pass a listener and set it to the click listener.
‫So I am referring to this item.
‫Click Listener.
‫This is a customized interface.
‫Third step modifying the view holder.
‫When the item view is clicked, you will call the method of the defined interface passing the clicked
‫items position as an argument.
‫So I need to go to here to the view Holder constructor and I'll set it to item view dot set on click
‫listener to this.
‫I know it may be confusing and a little bit complex and this I need to refer to as the implemented view
‫on Click Listener.
‫This is what I'm talking about.
‫View on Click Listener is an interface provided by the Android framework that defines a single method.
‫If we click on it Ctrl and Left Mouse, I'll go to the on click listener and you can see it's an interface
‫defined in this view package containing only one method on click, which gets invoked when a view or
‫UI element is clicked.
‫It's commonly used to set up click listeners for various UI elements such as buttons, text views,
‫images and so on, and we use it many in our previous applications.
‫When you implement the view dot onClick listener in this case in the view holder class and override
‫the on click method Alt+ enter to implement it and you can see it's here.
‫You're specifying what should happen when the associated view is clicked.
‫This interface allows you to create modular code that responds to user interactions in a clean and organized
‫manner.
‫Let's join this interface with our custom interface that we create.
‫If Click listener, I need to change my class that remove the static keyword and make it non-static.
‫So click listener In order to access the click listener.
‫Click listener doesn't equal to null.
‫I'm specifying and testing that it is not null.
‫Click listener dot on click.
‫Need two parameters to be passed.
‫The first parameter is the view, which is as a parameter on the click.
‫So I'm passing it here.
‫The second parameter is get adapter position.
‫It's of type integer and the get adapter position method is used to retrieve the adapter position of
‫the item associated with the view holder.
‫This method is particularly useful when you need to know the position of the clicked item within the
‫data set.
‫The last step is to go to the main activity and link the adapter with the click listener.
‫So adapter dot, set, click listener to this and I need to implement the click listener that I created
‫before Implements item click Listener Alt+ Enter implement the method called on click and you can see
‫this is my method Capital C and Capital L and you can see that I am implementing this method that is
‫associated with the view dot on click.
‫Listener interface.
‫So I am assigning my custom interface to this.
‫Interface that handles the click events on the View.
‫And I need to display a toast message telling us the the name of the chosen category list item list
‫dot dot get.
‫I need to pass position dot get item name run.
‫And congratulations guys.
‫We succeeded in getting and handling the click events on the items of the recycler view.