Skip to main content

How to implement item swiping actions in a RecyclerView in Kotlin Android

How to implement item swiping actions in a RecyclerView in Kotlin Android.

Here's a detailed step-by-step tutorial on how to implement item swiping actions in a RecyclerView in Kotlin Android:

Step 1: Create a new Android project in Kotlin and add the necessary dependencies Open Android Studio and create a new project. Make sure to select Kotlin as the programming language. Once the project is created, open the build.gradle (Module: app) file and add the following dependencies:

dependencies {
implementation 'androidx.recyclerview:recyclerview:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
}

Sync your project to download the dependencies.

Step 2: Create the layout for the RecyclerView item

In the res/layout folder, create a new XML layout file called "item_layout.xml". This layout will represent a single item in the RecyclerView. Here's an example of a simple item layout containing a TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<TextView
android:id="@+id/itemText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp" />

</androidx.cardview.widget.CardView>

</LinearLayout>

Step 3: Create the RecyclerView adapter

Create a new Kotlin class called "RecyclerViewAdapter". This class will be responsible for populating the data and creating the views for each item in the RecyclerView. Extend the RecyclerView.Adapter class and implement the necessary methods. Here's an example of the RecyclerViewAdapter:

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewAdapter(private val itemList: List<String>) :
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {

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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = itemList[position]
holder.itemText.text = item
}

override fun getItemCount(): Int {
return itemList.size
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val itemText: TextView = itemView.findViewById(R.id.itemText)
}
}

Step 4: Implement item swiping actions in the RecyclerView

To implement item swiping actions in the RecyclerView, we'll use the ItemTouchHelper class provided by the Android Support Library. Modify your activity or fragment class that contains the RecyclerView to add item swiping functionality. Here's an example of how to do it:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView
private lateinit var adapter: RecyclerViewAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val itemList = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")

recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
adapter = RecyclerViewAdapter(itemList)
recyclerView.adapter = adapter

val itemTouchHelperCallback = object : ItemTouchHelper.SimpleCallback(
0,
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
// Perform the desired action when an item is swiped
// For example, you can remove the item from the list
itemList.removeAt(position)
adapter.notifyItemRemoved(position)
}
}

val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
itemTouchHelper.attachToRecyclerView(recyclerView)
}
}

In this example, we create an ItemTouchHelperCallback that defines which swiping directions are allowed and what action should be performed when an item is swiped. We then attach the ItemTouchHelper to the RecyclerView.

That's it! You have successfully implemented item swiping actions in a RecyclerView using Kotlin in Android.