Handling Click Events in RecyclerView – Item Click, Long Click, and Best Practices (Kotlin & Java)

Learn how to handle click events in Android RecyclerView. Implement item click, long click, and best practices using Kotlin and Java.

Handling Click Events in RecyclerView – Item Click, Long Click, and Best Practices (Kotlin & Java)

Introduction

RecyclerView is one of the most widely used UI components in Android apps. But displaying data alone is not enough. Users must be able to interact with the list items.

Common interactions include:

  • Clicking a product

  • Opening a chat message

  • Editing a billing item

  • Deleting a record

  • Showing item details

Handling these interactions requires proper click event implementation in RecyclerView.

Let’s understand how to do it correctly.


Why Click Handling is Different in RecyclerView

Unlike a normal Button, RecyclerView items are dynamically created and recycled.

That means:

Views reused

Positions change

Click handling must be dynamic

If not implemented correctly, clicks may trigger wrong items.


Basic Approach – Click Inside ViewHolder

The simplest approach is setting a click listener inside onBindViewHolder.

Example (Kotlin):

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

holder.textView.text = items[position]

holder.itemView.setOnClickListener {

println("Clicked item: ${items[position]}")
}
}

This detects which item was clicked.


Java Example

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

holder.text.setText(items.get(position));

holder.itemView.setOnClickListener(v -> {

System.out.println("Clicked item: " + items.get(position));
});
}

This works but is not ideal for large apps.


Best Practice – Use Interface Callback

Professional Android apps use interfaces to handle clicks.

This separates UI logic from Adapter.

Example structure:

Activity

Adapter

Click Interface

Cleaner architecture.


Step 1 – Create Click Interface

Example:

interface OnItemClickListener {

fun onItemClick(position: Int)
}

This defines click action.


Step 2 – Pass Listener to Adapter

Adapter constructor:

class ProductAdapter(
private val items: List<String>,
private val listener: OnItemClickListener
) : RecyclerView.Adapter<ProductAdapter.ViewHolder>()

Now adapter can communicate with Activity.


Step 3 – Handle Click in ViewHolder

Example:

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

init {
view.setOnClickListener {

listener.onItemClick(adapterPosition)
}
}
}

Click is forwarded to Activity.


Step 4 – Implement Listener in Activity

Example:

class MainActivity : AppCompatActivity(), OnItemClickListener {

override fun onItemClick(position: Int) {

println("Item clicked: $position")
}
}

Now Activity controls click behavior.


Handling Long Click

Sometimes you want long press actions.

Example:

  • Delete item

  • Show options

  • Select multiple items

Example:

holder.itemView.setOnLongClickListener {

println("Long clicked: ${items[position]}")

true
}

Returning true confirms the event was handled.


Example – POS Billing Item Click

Imagine POS billing list.

When user clicks an item:

Product Name | Qty | Price

Click action:

Open edit quantity dialog

RecyclerView adapter sends click event to Activity.

Activity opens edit screen.


Example – Chat App

Chat list:

User1
User2
User3

Click action:

Open chat conversation

RecyclerView adapter triggers navigation.


Selecting Items in RecyclerView

Sometimes list items must highlight when clicked.

Example:

Selected item background changes

Implementation:

holder.itemView.setBackgroundColor(Color.LTGRAY)

Useful for:

  • Multi-select lists

  • File managers

  • Email apps


Handling Button Click Inside RecyclerView Item

Example item layout:

Product Name
Price
[Delete Button]

Example:

holder.deleteButton.setOnClickListener {

listener.onDeleteClick(position)
}

RecyclerView supports multiple click types.


AdapterPosition vs Position

Important concept.

Wrong:

position

Better:

adapterPosition

Because items may move or change.

adapterPosition always reflects correct position.


Common Beginner Mistakes

❌ Using static position references
❌ Handling clicks only inside Adapter
❌ Not separating UI logic
❌ Forgetting adapterPosition

Best practice:

Use interface callbacks.


Interview-Level Answer

If interviewer asks:

“How do you handle item clicks in RecyclerView?”

Professional answer:

Item clicks can be handled by attaching listeners inside the ViewHolder or onBindViewHolder, and best practice is using interface callbacks to communicate click events from Adapter to Activity or Fragment.


Simple Summary

RecyclerView click flow:

User taps item

ViewHolder detects click

Adapter forwards event

Activity handles action

Clean and scalable approach.


Conclusion

Handling click events properly in RecyclerView is essential for interactive Android apps. Whether it’s opening a detail screen, editing a record, or deleting an item, RecyclerView click handling connects UI interaction with application logic.

Using interfaces and proper architecture ensures your code remains clean, scalable, and maintainable.

Once you master RecyclerView interactions, building complex user interfaces becomes much easier.

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0