ItemTouchHelper.SimpleCallback swipe programmatically

Viewed 304

I am using android studios ItemTouchHelper.SimpleCallback to swipe items, I want items to be swiped on long item hold, is there a way to programmatically swipe an item? how can I implement this functionality?

1 Answers

Define the required animation(s) in res/anim folder.

left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate android:fromXDelta="-100%" android:toXDelta="0%"
             android:fromYDelta="0%" android:toYDelta="0%"
             android:duration="700"/>
</set>

right_to_left.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate
     android:fromXDelta="0%" android:toXDelta="100%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700" />
</set>

Now, just play the animation on the method where you detect the long press like this:

Animation slideAnim = AnimationUtils.loadAnimation(this, R.anim.right_to_left);
view.startAnimation(slideAnim); 
// Where 'view' is the view object which you want to swipe
// e.g: if its a list item you want to swipe, then maybe the 'view' can be the cardview that wraps the list item.

Hope this helps.

Related