Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement slide to show actions in recyclerview?

I have an app with a recyclerview showing some elements, the recyclerview also supports multiple viewtypes that can be changed from the settings. I am now looking to implement a way to show some actions for the recyclerview-items when a user swipes the item. Specifically I was looking for a way to implement a pane sliding out with the actions similar to the relay for reddit app.

The only answer I could find more or less, was to add a viewpager to each row in the recyclerview, but this doesn't seem like a very clean solution, especially with several viewtypes. How can I implement this feature?

like image 282
Cédric Avatar asked Apr 04 '15 21:04

Cédric


People also ask

How do I use ItemTouchHelper Simplecallback?

A simple wrapper to the default Callback which you can construct with drag and swipe directions and this class will handle the flag callbacks. You should still override onMove or onSwiped depending on your use case. final int fromPos = viewHolder.

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.


1 Answers

The correct/recommended way to add actions on RecyclerView items on user swipe is to use a ItemTouchHelper.

You could add a swipe action like this:

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

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            //Perform your action
        }
    });
    itemTouchHelper.attachToRecyclerView(recyclerView);
like image 68
nikoker Avatar answered Oct 21 '22 19:10

nikoker