Android "swipe left to right to delete", gesture on list item, ICS Style
Asked Answered
S

2

8

I am trying to implement the "swipe left to right to delete" gesture that is present for the notifications in Android ICS and above. I have a listview in my application. I have the gesture detector working. BUT when i swipe from left to right on a particular list item, I want the item to move along with my finger.When I move my finger, beyond a certain point, only then should the item delete itself.The OnFling() method I have currently does not achieve this.

How can i modify the OnFling() method to make the item move along with my finger ?

My Gesture listener class is

class GestureListener extends SimpleOnGestureListener {
        private static final int SWIPE_MIN_DISTANCE = 50;
        private static final int SWIPE_MAX_OFF_PATH = 100;
        private static final int SWIPE_THRESHOLD_VELOCITY = 25;

        private MotionEvent mLastOnDownEvent = null;

        @Override
        public boolean onDown(MotionEvent e) {
            mLastOnDownEvent = e;
            return super.onDown(e);
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "On Single TAP up ", Toast.LENGTH_SHORT).show();
            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e1 == null) {
                e1 = mLastOnDownEvent;
            }
            if (e1 == null || e2 == null) {
                return false;
            }

            float dX = e2.getX() - e1.getX();
            float dY = e1.getY() - e2.getY();

            if (Math.abs(dY) < SWIPE_MAX_OFF_PATH
                    && Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY
                    && Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
                if (dX > 0) {
                    int position = tasks.pointToPosition((int) e1.getX(),
                            (int) e1.getY());

                    int _id = (int) tasks.getItemIdAtPosition(position);
                    databaseConnector.deleteContact(_id);

                    new DeleteRow(_id, contactAdapter, getApplicationContext());
                contactAdapter.notifyDataSetChanged();


                    Toast.makeText(getApplicationContext(),
                            "Right Swipe" + _id, Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(getApplicationContext(), "Left Swipe",
                            Toast.LENGTH_SHORT).show();
                }
                return true;
            }

            return false;
        }
Sosthina answered 25/2, 2013 at 2:22 Comment(1)
If you have not done so already, you might want to peek at Roman Nurik's implementation: plus.google.com/113735310430199015092/posts/Fgo1p5uWZLu or Jake Wharton's backport using NineOldAndroids: github.com/JakeWharton/SwipeToDismissNOAHutson
C
5

You should have a look at Roman Nurik's swipe to dismiss snippet at github if you want to implement this type of functionality. You can find it here.

Cluck answered 25/2, 2013 at 2:26 Comment(2)
The author of Swipe-To-Dismiss (Tim Roes) has published a new version called EnhancedListView : github.com/timroes/EnhancedListView. You can even download a demo from Play Store.Cuticle
I am using the same but listview item is not sliding smoothly I mean if user swipe slightly downward then swipe action is not detected. Any help is appreciated. Thanks in advanceFavien
T
2

If you are able to use a more recent support library, RecyclerView has some built-in functionality in the form of ItemTouchHelper and the onSwiped method that can give you the desired behavior.

That support started in v22.2.0 of the RecyclerView support library according to this very helpful answer: https://mcmap.net/q/181766/-swipe-to-dismiss-for-recyclerview-closed

Talos answered 9/12, 2015 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.