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

newbie picture newbie · Feb 25, 2013 · Viewed 21.2k times · Source

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;
        }

Answer

ebarrenechea picture ebarrenechea · Feb 25, 2013

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.