How to implement multi-select in a RecyclerView in Kotlin Android
How to implement multi-select in a RecyclerView in Kotlin Android.
Here's a step-by-step tutorial on how to implement multi-select in a RecyclerView in Kotlin for Android.
Step 1: Set up your project
Start by creating a new project in Android Studio and add the necessary dependencies in your app-level build.gradle file:
implementation 'androidx.recyclerview:recyclerview:1.2.1'
Step 2: Create the RecyclerView layout
Create a new XML layout file for the RecyclerView, which will be used to display the list items. For example, create a file called item_list.xml and add the following code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item"
android:padding="8dp" />
</LinearLayout>
Step 3: Create the RecyclerView adapter
Create a new Kotlin file for the RecyclerView adapter, which will handle the data binding and view creation. For example, create a file called ListAdapter.kt and add the following code:
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.RecyclerView
class ListAdapter(private val items: List<String>) :
RecyclerView.Adapter<ListAdapter.ViewHolder>() {
private val selectedItems = HashSet<Int>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_list, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.checkBox.text = item
holder.checkBox.isChecked = selectedItems.contains(position)
holder.checkBox.setOnClickListener {
if (selectedItems.contains(position)) {
selectedItems.remove(position)
} else {
selectedItems.add(position)
}
notifyDataSetChanged()
}
}
override fun getItemCount(): Int {
return items.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val checkBox: CheckBox = itemView.findViewById(R.id.item_checkbox)
}
fun getSelectedItems(): Set<Int> {
return selectedItems
}
}
Step 4: Set up the RecyclerView in your activity
In your activity layout XML file, add a RecyclerView element. For example, add the following code to your activity's XML layout file:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then, in your activity's Kotlin file, initialize the RecyclerView and set up the adapter. Add the following code to your activity:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: ListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
val items = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
adapter = ListAdapter(items)
recyclerView.adapter = adapter
}
}
Step 5: Handle selected items
To retrieve the selected items from the RecyclerView, you can call the getSelectedItems() function from the adapter. For example, you can add a button in your activity layout and handle the click event to get the selected items:
val selectedItems = adapter.getSelectedItems()
That's it! You have now implemented multi-select in a RecyclerView in Kotlin for Android. You can customize the layout and functionality as per your requirements.