Swipe to delete listitem

I would like to implement a swipe gesture to delete rows in a ListView similar to the android notifications.

Right now all I have is a ListView with an onTouchListener – that said, I already have swipe detection working.

  • Seek Bar thumb not transparent in Android 5.0 API 21 Lollipop
  • Listen to volume buttons in background service?
  • What is the difference between setFocusable and setFocusableInTouchMode?
  • How to run command 'ionic run android' on specific device?
  • Android Studio 2.0 launch APK on another device
  • How to allow full values (integers) only in ratingbars?
  • gestureDetector = new GestureDetector(this, new GestureListener());
    onTouchListener = new TouchListener();  
    listview.setOnTouchListener(onTouchListener);  
    

    My GestureListener class:

    protected class GestureListener extends SimpleOnGestureListener
    {
        private static final int SWIPE_MIN_DISTANCE = 150;
        private static final int SWIPE_MAX_OFF_PATH = 100;
        private static final int SWIPE_THRESHOLD_VELOCITY = 100;
    
        private MotionEvent mLastOnDownEvent = null;
    
        @Override
        public boolean onDown(MotionEvent e)
        {
            mLastOnDownEvent = e;
            return super.onDown(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) {
                    Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
            else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH && Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
                if (dY>0) {
                    Toast.makeText(getApplicationContext(), "Up Swipe", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Down Swipe", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
            return false;
        }
    }
    

    My TouchListener class:

    protected class TouchListener implements View.OnTouchListener
    {
        @Override
        public boolean onTouch(View v, MotionEvent e)
        {
            if (gestureDetector.onTouchEvent(e)){
                return true;
            }else{
                return false;
            }
        }
    }
    

    Are there some tutorials / examples on that?

    thanks

    Related posts:

    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open dat...
    What&#039;s the core difference between fragment and activity? Which code can be written in frag...
    Android: Opening SMS activity with multiple recipients specified
    How do I check to see if a resource exists in Android
    Android Network Service Discovery &ldquo;timeout&rdquo;?
    launch activities from different package
  • ActivityNotFoundException when different package's targetClass in PreferenceScreen
  • Adding watermark bitmap over video in android: 4.3's MediaMuxer or ffmpeg
  • Spring MVC, Thymeleaf & REST
  • What is the difference between min SDK version/target SDK version vs. compile SDK version?
  • What's LazyList?
  • Force Android to use 3G when on local area wifi without net access
  • 4 Solutions collect form web for “Swipe to delete listitem”

    If you have your swipe detection working, all that is left is to delete the item. For that, the following code will delete the item off screen.

    yourListViewAdapter.yourListItems.remove(position);
    yourListViewAdapter.notifyDataSetChanged();
    

    You can achieve a nice effect by adding this to your swipe detection:

                //if swipe to left detected
                Display display = getWindowManager().getDefaultDisplay();
                v.clearAnimation();
                TranslateAnimation translateAnim = new TranslateAnimation(0, -display.getWidth(), 0, 0);
                translateAnim.setDuration(250);
                translateAnim.setAnimationListener(new Animation.AnimationListener() {
    
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
    
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
    
                    @Override
                    public void onAnimationEnd(Animation animation) {   
                        yourListViewAdapter.yourListItems.remove(position);
                        yourListViewAdapter.notifyDataSetChanged();
                    }
                });
                v.startAnimation(translateAnim);
    

    i guess i really have to implement a touch listener on every row in the list. -> look for custom ArrayAdapter

    as for throwing items, i found a great tutorial that answers most of my questions: http://mobile.tutsplus.com/tutorials/android/android-gesture/

    In my search for some kind of swipe listener, I came across Roman Nurik’s Swipe code. [1]: https://github.com/romannurik/android-swipetodismiss

    I’ve been using this in my app and it works like a charm!

    It’s written in the same way an implemented listener would so I found it easy to work with.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.