Android Academy · Lesson

RecyclerView Click Events

Handle item clicks and long presses in RecyclerView, pass click listeners to adapters, and navigate to a detail screen on item tap.

Lesson 3 of 611 steps

RecyclerView Click Events is a free Android Academy lesson on CoddyKit. This is lesson 3 of 6. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 6 lessons in total.

Making List Items Clickable

RecyclerView doesn't have a built-in click listener like ListView did. You add click listeners inside the Adapter.

The recommended pattern: pass a lambda (callback) to the Adapter's constructor. When an item is tapped, the Adapter calls the lambda with the selected item — the Activity handles what happens next.

Adapter with Click Callback

Add an onItemClick lambda parameter to the Adapter:

class FruitAdapter(
    private val fruits: List<Fruit>,
    private val onItemClick: (Fruit) -> Unit
) : RecyclerView.Adapter<FruitAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvName: TextView = view.findViewById(R.id.tvName)
        val tvCalories: TextView = view.findViewById(R.id.tvCalories)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_fruit, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = fruits[position]
        holder.tvName.text = fruit.name
        holder.tvCalories.text = "${fruit.calories} kcal"
        // Set click listener on the whole item
        holder.itemView.setOnClickListener {
            onItemClick(fruit)
        }
    }

    override fun getItemCount() = fruits.size
}

Handling Clicks in the Activity

In the Activity, pass a lambda that navigates to the detail screen:

val adapter = FruitAdapter(fruits) { fruit ->
    // Called when user taps a fruit
    val intent = Intent(this, FruitDetailActivity::class.java)
    intent.putExtra("FRUIT_NAME", fruit.name)
    intent.putExtra("FRUIT_CALORIES", fruit.calories)
    startActivity(intent)
}

binding.recyclerView.apply {
    this.adapter = adapter
    layoutManager = LinearLayoutManager(this@MainActivity)
}

Receiving Data in Detail Screen

Read the extras in the detail Activity:

class FruitDetailActivity : AppCompatActivity() {

    private lateinit var binding: ActivityFruitDetailBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFruitDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val name = intent.getStringExtra("FRUIT_NAME") ?: "Unknown"
        val calories = intent.getIntExtra("FRUIT_CALORIES", 0)

        binding.tvDetailName.text = name
        binding.tvDetailCalories.text = "$calories kcal"
    }
}

DiffUtil for Efficient Updates

When your list data changes, don't call notifyDataSetChanged() — it redraws everything. Use DiffUtil instead:

  • Calculates the minimal diff between old and new lists
  • Animates only the changed items
  • Better performance and UX

The modern way is ListAdapter which uses DiffUtil automatically.

ListAdapter with DiffUtil

Extend ListAdapter for automatic, animated list updates:

class FruitAdapter(
    private val onItemClick: (Fruit) -> Unit
) : ListAdapter<Fruit, FruitAdapter.ViewHolder>(DIFF_CALLBACK) {

    companion object {
        val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Fruit>() {
            override fun areItemsTheSame(old: Fruit, new: Fruit) =
                old.name == new.name
            override fun areContentsTheSame(old: Fruit, new: Fruit) =
                old == new  // data class equals()
        }
    }

    // ... ViewHolder and onCreateViewHolder same as before ...

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = getItem(position)  // use getItem(), not fruits[position]
        holder.tvName.text = fruit.name
        holder.itemView.setOnClickListener { onItemClick(fruit) }
    }
}

Submitting New Lists

With ListAdapter, update the list by calling submitList(). DiffUtil handles the animation automatically:

// In Activity:
val adapter = FruitAdapter { fruit ->
    // handle click
}
binding.recyclerView.adapter = adapter

// Initial data
adapter.submitList(fruits)

// Later, update with new data (e.g. after filtering):
val filtered = fruits.filter { it.calories < 100 }
adapter.submitList(filtered)  // DiffUtil animates the change

Long Press & Context Menus

You can also respond to long presses by setting setOnLongClickListener in onBindViewHolder:

  • Return true from the listener to consume the event
  • Use it to show a delete/edit dialog
  • Or implement swipe-to-delete with ItemTouchHelper

Adding Item Decorations

Add visual separation between list items with DividerItemDecoration:

binding.recyclerView.apply {
    adapter = fruitAdapter
    layoutManager = LinearLayoutManager(this@MainActivity)
    addItemDecoration(
        DividerItemDecoration(
            this@MainActivity,
            DividerItemDecoration.VERTICAL
        )
    )
}

Quick Check

What is the recommended way to handle item clicks in a RecyclerView Adapter?

Recap: RecyclerView Clicks

You can now build interactive lists:

  • Pass a lambda callback to the Adapter constructor
  • In onBindViewHolder, call holder.itemView.setOnClickListener
  • Navigate to a detail screen by passing extras via Intent
  • Use ListAdapter + DiffUtil for smooth, animated updates
  • Call submitList() to update the displayed data

Next: persist data locally with SharedPreferences.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “RecyclerView Click Events” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “RecyclerView Click Events”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 6 lessons in total.

What will I learn in “RecyclerView Click Events”?

Handle item clicks and long presses in RecyclerView, pass click listeners to adapters, and navigate to a detail screen on item tap. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 6.

How long does the “RecyclerView Click Events” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Kotlin Collections
  2. RecyclerView Basics
  3. RecyclerView Click Events
  4. SharedPreferences
  5. DataStore Preferences
  6. Adapters & DiffUtil
← Back to Android Academy