chris
chris

Reputation: 1811

How to programmatically initiate a swipe to dismiss with RecyclerView

When I delete an item from a RecyclerView I want to show a "swipe" animation, that is that the item is moved sideways off the screen. I believe there is support for "swipe to dismiss" using ItemTouchHelper, but this is touch-initiated whereas I want to be able to initiate the swipe programmatically.

I have also tried setting the RecyclerView item animator by extending DefaultItemAnimator. Using this method I can get items to swipe left and right but unfortunately the gap in the list closes quickly such that the swipe does not finish before the list item gap has closed.

Anyone know how to do this?

Upvotes: 3

Views: 3500

Answers (2)

Zain
Zain

Reputation: 40830

Although it's already answered with external library; probably someone wants to do it with raw java.. I have done it with the solution in here

The idea is that, you take off the list item view of the RecyclerView to be deleted and animate it; and normally remove it from the adapter

private void removeListItem(View rowView, final int position) {

Animation anim = AnimationUtils.loadAnimation(this,
        android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);

new Handler().postDelayed(new Runnable() {

    public void run() {

        values.remove(position); //Remove the current content from the array

        adapter.notifyDataSetChanged(); //Refresh list
    }

}, anim.getDuration());
}

Upvotes: 0

Ari
Ari

Reputation: 3086

You can use ItemAnimators offered by this library. More specifically, your would use their SlideInLeftAnimator or SlideInRightAnimator.

Upvotes: 1

Related Questions