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.
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:
↓
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):
holder.textView.text = items[position]
holder.itemView.setOnClickListener {
println("Clicked item: ${items[position]}")
}
}
This detects which item was clicked.
Java Example
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:
↓
Adapter
↓
Click Interface
Cleaner architecture.
Step 1 – Create Click Interface
Example:
fun onItemClick(position: Int)
}
This defines click action.
Step 2 – Pass Listener to Adapter
Adapter constructor:
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:
init {
view.setOnClickListener {
listener.onItemClick(adapterPosition)
}
}
}
Click is forwarded to Activity.
Step 4 – Implement Listener in Activity
Example:
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:
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:
Click action:
RecyclerView adapter sends click event to Activity.
Activity opens edit screen.
Example – Chat App
Chat list:
User2
User3
Click action:
RecyclerView adapter triggers navigation.
Selecting Items in RecyclerView
Sometimes list items must highlight when clicked.
Example:
Implementation:
Useful for:
-
Multi-select lists
-
File managers
-
Email apps
Handling Button Click Inside RecyclerView Item
Example item layout:
Price
[Delete Button]
Example:
listener.onDeleteClick(position)
}
RecyclerView supports multiple click types.
AdapterPosition vs Position
Important concept.
Wrong:
Better:
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:
↓
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
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0