make recycler view item swipe only partially

Updated 13 April 2017

Save

In last blog, we discussed how to make items in your recycler view swipeable , now we are going to discuss how to make this happen partially and display some other thing say (png file) in place of the displayed view.

A perfect example for this would be deleting the item from recycler view list with the permission of the user.

If you have followed the last blog then implementing this is pretty easy as there is nothing new this time.

First create a simple callback of the ItemTouchHelper class, override its onSwiped and onChildDraw method, and attach it to your recycler view and its done .

Now lets have a look at the code :

Explanation :

This time there is nothing new except that we have set the TranslationX value to dX/5 .

The dX value tells you about the direction in which your item has moved and it ranges from 0 (not moved) to 1(fully swiped) . And the sign before the dX value tells you about the direction (-ve means item has moved left and +ve means item has moved right).
This being said i have used the dx/5 value as it suited my case, you can choose any value depending on your use case.

Now the second important change that you will notice is in the line where “icon” is initialized , here we have decoded a png file into corresponding bitmap and then displayed it in a rectangle with name “icon_dest” and when you will see the “icon_dest” initialization line , you will notice the bounds that i have made, these are just to make the icon appear in center and good looking with my recycler view item.

Now you just need to attach this code to your recycler view,
For this you just need to add two lines in your piece of code :

And all is done , build your project and see how it looks and change the calculaltions as per your need 🙂

Keep coding and Keep sharing 🙂

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home