Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
        }
like image 458
newbie Avatar asked Feb 25 '13 02:02

newbie


2 Answers

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.

like image 192
ebarrenechea Avatar answered Oct 13 '22 09:10

ebarrenechea


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://stackoverflow.com/a/30601554/1518546

like image 26
John Cummings Avatar answered Oct 13 '22 11:10

John Cummings