Long click recyclerview android studio

RecyclerView Item Click Listener the Right Way

Some Android developers wonder why Google created a view like Recyclerview without a click listener(given the fact that the deprecated ListView has an item click listener).

Because there is no standard way of setting a click listener, new developers tend to confuse on the right way of doing it. In this article, I will show you how to do it in a proper way using an example scenario.

Example Scenario

  • There is a Recyclerview adapter with a Recyclerview with a list of items(Users in this case).
  • What we want is that when an item is clicked, we get the item’s model(User) information and may be pass it to a second activity.

Project Creation

From Android Studio, create an empty android project(Select the Kotlin support option) and name your activity, MainActivity.

  • Delete the default ‘Hello World’ TextView in activity_main.xml .
  • Add recyclerview and cardview dependencies in app level build.gradle file as shown below.

Add recyclerview in activity_main.xml where you removed the textview as shown below.

Ok we are good to go. The assumption is that you have worked with the recyclerview(in Java) before and know how to create a recyclerview adapter.. Next, we create the model class containing username and phone.

Читайте также:  Север ру для андроида

Data model

Sweet Kotlin. Just the above line of code gives us access to a setter and getter under the hood and other methods like toString. Check more at https://kotlinlang.org/docs/reference/data-classes.html .

Next, we create our view holder and adapter. Pay attention here because it is the most important part of what this article addresses.

Recyclerview Adapter and view holder

  • Create a new Kotlin file called RecyclerAdapter.kt
  • Next we create our item_user.xml layout as follows

The item_user has two textviews which holds the name and phone.

Next we create our view holder. As usual, our view holder has a constructor with an itemView as parameter and we get a reference to our views from item_user layout .

Then we create our adapter with the list of users as a parameter. An adapter contains the list of users

Item Click the bad way

Note that in onBindView, we can have access to the the current clicked item and thus open a new activity from here and pass our data..

This will work perfectly but it is a bad practice because

  • It is not a good practice opening an Activity from a viewholder context
  • Note that onBindViewHolder is called as your views are populated during scrolling. Thus you will have numerous calls to setOnClickListener.

Let us fix it

The way that you should do is that you create an ItemClickListener interface with one method ontemClicked with parameter User.

  • We then pass modify the Adapter’s constructor to take the users list and an OnItemClickListener interface
  • We also modify the the ViewHolder to have a bind function which takes a user and itemClick interface as follows.
Читайте также:  Fps meter для android

This is all we have to do. We just have to implement the interface in our MainActivity .

Источник

Ready Android

Pursuing Perfection Passionately

Thanks to

Evergreen Posts & Pages

Nice to see u 🙂

Single item click and long press in RecyclerView/ListView: Android

Creating Interface

I hope you have read my previous article. Let me take you from there. Under MainActivity, We need to create a simple interface for single tap and for long press events, by passing “view” and its “position” as parameters

Creating InnerClass

Under MainActivity, Let’s create an inner class implementing RecyclerView.OnItemTouchListener. We also need to make use of ClickListener interface in this inner class. GestureDetector class is used to listen for various touch events.

Adding Click Listeners to RecyclerView

Now let’s add the ItemTouchListener to the RecyclerView, where we will pass RecyclerTouchListener class. This will implement onClick and onLongClick methods.These methods will listen for the click and long press events on the particular position of the view.

So….. our Listeners are all ready.

Accessing Particular View Component in the RecyclerView Item

In order to access particular view component inside RecyclerView, for example, In order to perform some operation only on clicking the profile image in the RecyclerView row item, then here is the small hack for you.

We can define the component by findViewById via it’s respective View. Here is the code for you.

Now you can perform any operation on clicking the image. Follow the same for the other view components.

Источник

Оцените статью