Skip to content
[ANDROID] Sample how to use onClickItem in RecyclerView
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
gradle/wrapper
.gitignore
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

Sample how to use onClickItem in RecyclerView

The idea of this project is showing how to create a simple onClickItem in RecyclerView using Kotlin.

This project uses libraries of the JetPack/Ktx. See: build.gradle

Basics Steps:

  • Configure your item.xml to clickable:
        android:clickable="true"
        android:focusable="true"
        android:background="?android:attr/selectableItemBackground"
  • Define your Listener in your Adapter
    class UserAdapter(
        //I decided to create a functional interface instead of a new class.
        val onClickItem: (view: View, user: User) -> Unit
    ) : ListAdapter<User, UserViewHolder>(UserDiffCallback()) {
  • On binding item, create the OnClickListener with the new action
 override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        getItem(position)?.let { user ->
            holder.bind(user, createOnClickListener(user))
        }
    }
    
 private fun createOnClickListener(user: User): View.OnClickListener {
        return View.OnClickListener { view ->
            //Here, We call the functional interface.
            onClickItem(view, user)
        }
    }
  • On bind method of the holder set the onClickListener in your itemView:
    fun bind(user: User, onClickListener: View.OnClickListener) {
        // [...] Binding values...
        itemView.setOnClickListener(onClickListener)
    }
  • At last, implement in your Activity:
    private fun createAdapter(): UserAdapter {
        return UserAdapter { view: View, user: User ->
            //TODO onClickListener
            Snackbar.make(view, user.toString(), Snackbar.LENGTH_LONG).show()
        }
    }
You can’t perform that action at this time.