Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do swipe gesture on RecyclerView item without 3rd party lib

Is their any direct support for slide to delete/archive (right to left or left to right) on RecyclerView item.

And instead of delete/archive I want four buttons under the list item.

something like this https://github.com/47deg/android-swipelistview but for recyclerview and official support not any 3rd party lib

like image 438
T_C Avatar asked Jan 22 '15 01:01

T_C


People also ask

What is viewType in onCreateViewHolder?

This viewType variable is internal to the Adapter class. It's used in the onCreateViewHolder() and onBindViewHolder to inflate and populate the mapped layouts. Before we jump into the implementation of the Adapter class, let's look at the types of layouts that are defined for each view type.

How do I drag and drop items in RecyclerView Android?

Drag and Drop can be added in a RecyclerView using the ItemTouchHelper utility class. Following are the important methods in the ItemTouchHelper. Callback interface which needs to be implemented: isLongPressDragEnabled - return true here to enable long press on the RecyclerView rows for drag and drop.


2 Answers

Yes there is, you can do it with ItemTouchHelper class provided by the support library.

P.S. I had to do this the other day and also wanted to avoid using 3rd party lib if possible. The library might be doing much more than you need and because of that it might be more complex than necessary in your case. It can also unnecessary grow your method count. This is just a sample of reasons why you should avoid adding lib as a quick fix for your problem.

EDIT: I had a go at this, see this blog post and this github repo.

like image 174
Nemanja Kovacevic Avatar answered Oct 10 '22 08:10

Nemanja Kovacevic


Yes there is. Use ItemTouchHelper. Try to clone this project and see how it is used.

For specific file, see line 87

For lazy people who don't want to click links, this is how you setup:

    ItemTouchHelper.SimpleCallback simpleCallback =
            new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
                @Override
                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                                      RecyclerView.ViewHolder target) {
                    return false;
                }

                @Override
                public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                    //do things
                }
            };

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);

The recyclerView is the variable holding recycler view.

There are other directions aside from ItemTouchHelper.RIGHT, try to experiment.

like image 43
hehe Avatar answered Oct 10 '22 09:10

hehe