Cédric
Cédric

Reputation: 419

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?

Upvotes: 14

Views: 4781

Answers (1)

nikoker
nikoker

Reputation: 41

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);

Upvotes: 2

Related Questions