Android RecyclerView Tutorial with Kotlin

In this Android RecyclerView tutorial, learn how to use Kotlin to display datasets of a large or unknown size! By Kevin D Moore.

4.7 (63) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Using ItemTouchHelper

Sometimes you’ll see things you just don’t like, such as a galaxy far, far away that has fallen to the dark side or a planet that is prime for destruction. How could you go about killing it with a swipe?

Luckily, Android has provided the ItemTouchHelper class, which allows for easy swipe behavior. Creating and attaching this to a RecyclerView requires just a few lines of code.

In MainActivity.kt, underneath setRecyclerViewScrollListener() add the following method:

private fun setRecyclerViewItemTouchListener() {

  //1
  val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, viewHolder1: RecyclerView.ViewHolder): Boolean {
      //2
      return false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) {
      //3
      val position = viewHolder.adapterPosition
      photosList.removeAt(position)
      recyclerView.adapter!!.notifyItemRemoved(position)
    }
  }

  //4
  val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
  itemTouchHelper.attachToRecyclerView(recyclerView)
}

Here’s the breakdown, step by step:

  1. Create the callback and tell it what events to listen for. It takes two parameters: One for drag directions and one for swipe directions. You’re only interested in swipe. Pass 0 to inform the callback not to respond to drag events.
  2. Return false in onMove. You don’t want to perform any special behavior here.
  3. Call onSwiped when you swipe an item in the direction specified in the ItemTouchHelper. Here, you request the viewHolder parameter passed for the position of the item view, and then you remove that item from your list of photos. Finally, you inform the RecyclerView adapter that an item has been removed at a specific position.
  4. Initialize ItemTouchHelper with the callback behavior you defined, and then attach it to the RecyclerView.

Add the method to the activity’s onCreate() underneath setRecyclerViewScrollListener():

setRecyclerViewItemTouchListener()

This will attach the ItemTouchListener to the RecyclerView using the code you just wrote.

Run the app once more and swipe across one of your items. You should see it begin to move. If you swipe the item far enough, you should see it animate and vanish. If other items are visible, they will reorganize themselves to cover the empty space. How cool is that?

13 Swipe Away Item

Where to Go From Here?

Nice job, Commander! Now it’s time to head back to Earth and think about what you’ve learned. In this tutorial, you:

  • Created a RecyclerView and all its necessary components, such as a LayoutManager, an Adapter and a ViewHolder.
  • Updated and removed items from an Adapter.
  • Added some cool features like changing layouts and adding swipe functionality.

Above all, you’ve now experienced how separating components — a key attribute of RecyclerViews — provides so much functionality with such ease. For flexible, exciting collections, look no further than the all-powerful RecyclerView.

Get the final project by using the Download Materials button at the top or bottom of this tutorial.

To learn more about RecyclerViews, check out the Android documentation. Take a look at the support library for RecyclerViews to learn how to use it on older devices. To make them fit with the material design spec, check out the list component design specification.

Join us in the forums to discuss this tutorial and share your findings as you work with RecyclerViews!

Until next time, Space Traveler!