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?
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With