0Pricing
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.

RecyclerView Click Events is a free Android Academy lesson on CoddyKit — lesson 3 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “RecyclerView Click Events” lesson free?

Yes — the full text of “RecyclerView Click Events” is free to read here on the web, and the Android Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 6, so you can start here or from the beginning and move at your own pace.

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