Skip to main content

How to implement swipe to mark as read functionality in a RecyclerView in Kotlin Android

How to implement swipe to mark as read functionality in a RecyclerView in Kotlin Android.

Here's a step-by-step tutorial on how to implement swipe to mark as read functionality in a RecyclerView in Kotlin Android.

Step 1: Set up the RecyclerView

First, you need to set up the RecyclerView in your layout file. Define a RecyclerView widget with an id and any other desired attributes. For example, in your XML layout file:

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Step 2: Create the RecyclerView Adapter

Next, create an adapter class that extends RecyclerView.Adapter. This adapter will be responsible for creating and binding the views for each item in the RecyclerView. Inside the adapter, create a ViewHolder class that extends RecyclerView.ViewHolder. This ViewHolder will hold references to the views of each item.

class MyAdapter(private val items: List<String>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
// Define views here
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// Inflate the item layout and create a ViewHolder
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Bind data to the views of the ViewHolder
val item = items[position]
// Update views here
}

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

Step 3: Implement ItemTouchHelper.Callback

To enable swipe functionality, you need to implement the ItemTouchHelper.Callback class. This class provides methods for handling swipe gestures on the RecyclerView items.

class SwipeToDeleteCallback(private val adapter: MyAdapter) : ItemTouchHelper.Callback() {

override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
return makeMovementFlags(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
// Handle the swipe action here
}
}

Step 4: Attach ItemTouchHelper to the RecyclerView

In your activity or fragment, instantiate the ItemTouchHelper and attach it to the RecyclerView.

val adapter = MyAdapter(items)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.adapter = adapter

val swipeToDeleteCallback = SwipeToDeleteCallback(adapter)
val itemTouchHelper = ItemTouchHelper(swipeToDeleteCallback)
itemTouchHelper.attachToRecyclerView(recyclerView)

Step 5: Handle the swipe action

Inside the onSwiped method of the SwipeToDeleteCallback class, you can handle the swipe action. In this example, let's mark the swiped item as read by changing its background color.

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
val item = items[position]

// Update the item's read status
// For example, change background color
viewHolder.itemView.setBackgroundColor(Color.GRAY)
}

That's it! You have successfully implemented swipe to mark as read functionality in a RecyclerView in Kotlin Android.