Part 2: RecyclerView from zero to hero

Alfredo Cerezo Luna
4 min readFeb 27, 2018

Giving it functionalities: swipe and drag & drop.

Thanks for staying with me!

What you will find here:

Now we are going to explore how we can modify our RecyclerView from Part 1 — First things first: implement a basic RecyclerView, to support swipe and drag & drop actions. To do that, we are going to use ItemTouchHelper, a utility class provided by Android which makes everything really easy.

ItemTouchHelper in deep

Lots of things are going on in this class, so let’s try to rip it and take a more detailed view of it.

As we saw in the previous post, a ViewHolder is going to present our information in each row of the list (items), if we want to interact with the elements within our list we need to play with it, and this is the purpose of the ItemTouchHelper class: to give us the tools and methods that make everything easier.

This class contains several things: methods provided by itself and, probably the most important part, a set of callbacks that serves as a contract between this class and our application.

Let’s start with the former:

ItemTouchHelper.Callback { ... }

We need to implement this interface and override some methods to enable/disable the actions that our users can perform, but also to receive some callbacks calls as responses of some actions.

First interesting method:

public abstract int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder);

Basically we have to tell the ItemTouchHelper if an item (ViewHolder) is swappable or draggable, to do that we will define some flags that the ItemTouchHelper will check, but these flags mean more, they also indicate the directions in which our items can be swiped (right or left) or moved (up or down), and we can even specify the direction in which an item can be moved when starting the action, and in which directions can be moved while performing the action. For instance: an item could be only swiped to the right, but once I start swiping, I can move it to the right or to the left.

And thanks to this static method, provided also by the Callback, this process is very simple and easy:

public abstract int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder);

Next interesting method inside the Callback:

public boolean canDropOver(RecyclerView recyclerView, ViewHolder current, ViewHolder target);

If you are just using only one type of ViewHolder (our example), most probably you don’t care about this, but the purpose of this method is to decide whether we can drop an item over another or we can not, for instance, if we don’t want to swipe positions between a regular item and a header inside our list. The easiest way to achieve that is checking in this method if both ViewHolders are of the same type (using instanceOf() is a good option in this case).

Another important method we need to implement to perform drag & drop:

public abstract boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target);

This method is called by the ItemTouchHelper when it wants to move the item we are dragging from one position to a new one.

In this example, the Callback delegates into our adapter how to handle these actions, but we will talk about this later.

And we have a similar method to announce that one item has been swiped:

public abstract void onSwiped(ViewHolder viewHolder, int direction)

Here you got the implementation:

That pretty much covers all the methods we need, but how theItemTouchHelper communicates with the adapter (our source of data for the RecyclerView)? We don’t know if the adapter is going to be always the same so it’s better to keep them decoupled, let’s create an interface for this purpose, we just need two methods: one for onMove() and one for onSwiped() :

and this is the interface definition:

Here you can find the implementation:

As I mentioned before in this example, the adapter is going to be the one who handles this changes in the data, so it needs to implement this interface. Resulting in the following:

And now we need to put everything together:

  • Create the callback.
  • Create the ItemTouchHelper from the callback.
  • Attach the RecyclerView to it.

And this is how it looks like:

Summary

We covered how to extend the functionalities of our RecyclerView, by adding swipe and drag & drop using ItemTouchHelper. To do that we implemented an ItemTouchHelper.Callback interface and created a helper interface that our adapter implements in order to communicate with it.

In the last post, we will cover how to draw a background when we swipe an element to give some meaning to our actions.

Continue reading

Part 3 — Painting a background while swiping.

Code

Thanks to @aconsuegra for reviewing this post.

--

--