h6ah4i / android-advancedrecyclerview

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
https://advancedrecyclerview.h6ah4i.com/
Apache License 2.0
5.32k stars 860 forks source link

How to reset the default position of swiped item? #311

Open Fuusio opened 7 years ago

Fuusio commented 7 years ago

First of all, thank you for the awesome library! I am a one happy user :)

Is there any method or way to reset the default position of a swiped item and with animation?

My use case is as follows. A user swipes a list item to reveal a set of actions buttons, and when the user clicks on any of the action, the item should be moved programmatically back to default position where it was before swiping.

For implementing that kind of behaviour, I already added method RecyclerViewSwipeManager#resetSwipedItemPosition(RecyclerView.ViewHolder swipedItem, int itemPosition) to my own fork, but I would prefer not to use fork if there is an already existing method to restore the default position.

Thank you in advance.

h6ah4i commented 7 years ago

Hi. Thank you for using my library!

Is there any method or way to reset the default position of a swiped item and with animation?

There is no special method exists for the purpose, but RecyclerView.Adapter.notifyItemChanged() and SwipeableItemViewHolder.setSwipeItemHorizontalSlideAmount() can be used.


  1. Clear "pinned" flag & notify item changes

    mAdapter.getItem( clickedItemIndex ).setPinned(false);
    mAdapter.notifyItemChanged( clickedItemIndex );
  2. Reset to default position in onBindViewHolder

    public void onBindViewHolder(MyViewHolder holder, int position) {
    final AbstractDataProvider.Data item = getItem(position);
    holder.setSwipeItemHorizontalSlideAmount(
           item.isPinned() ? Swipeable.OUTSIDE_OF_THE_WINDOW_LEFT : 0);
    ...
    }

Please refer to the "Swipeable (Basic)" demo implementation for more details. In the demo, same technique is used when clicking the yellow area of pinned item.

Thanks!