Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to detect swipe left or right function on listview item in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 8.19k
    Comment on it

    If you want to detect swipe gesture from left to right or right to left on the list view item in android. than you just have to add this Class in your activity and perform any task by detecting any type of gesture in your list item in android. Follow the instruction given below to achieve this functionality in you listview.

    This is the Class which you have to add in your activity

    public static enum Action {
        LR, // Left to right
        RL, // Right to left
        TB, // Top to bottom
        BT, // Bottom to top
        None // Action not found
    }
    public class SwipeDetector implements View.OnTouchListener {
    
        private static final int HORIZONTAL_MIN*DISTANCE = 30; // The minimum
        // distance for
        // horizontal swipe
        private static final int VERTICAL_MIN_DISTANCE = 80; // The minimum distance
        // for vertical
        // swipe
        private float downX, downY, upX, upY; // Coordinates
        private Action mSwipeDetected = Action.None; // Last action
    
        public boolean swipeDetected() {
            return mSwipeDetected != Action.None;
        }
        public Action getAction() {
            return mSwipeDetected;
        }
        /**
         * Swipe detection
         */@Override
         public boolean onTouch(View v, MotionEvent event) {
             switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
             {
                 downX = event.getX();
                 downY = event.getY();
                 mSwipeDetected = Action.None;
                 return false; // allow other events like Click to be processed
             }
             case MotionEvent.ACTION_MOVE:
             {
                 upX = event.getX();
                 upY = event.getY();
                 float deltaX = downX - upX;
                 float deltaY = downY - upY;
                 // horizontal swipe detection
                 if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
                     // left or right
                     if (deltaX < 0) {
                         mSwipeDetected = Action.LR;
                         return true;
                     }
                     if (deltaX > 0) {
                         mSwipeDetected = Action.RL;
                         return true;
                     }
                 } else
                     // vertical swipe detection
                     if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
                         // top or down
                         if (deltaY < 0) {
                             mSwipeDetected = Action.TB;
                             return false;
                         }
                         if (deltaY > 0) {
                             mSwipeDetected = Action.BT;
                             return false;
                         }
                     }
                 return true;
             }
             }
             return false;
         }
    
    }
    

    And apply it on in your listView setOnItemClickListener like this

       listView.setOnItemClickListener(new OnItemClickListener() {
         @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                if (swipeDetector.swipeDetected()) {
    
                    if (swipeDetector.getAction() == Action.LR) {
                        // perform any task
                    }
                    if (swipeDetector.getAction() == Action.RL) {
                        // perform any task
                    }
                    if (swipeDetector.getAction() == Action.TB) {
                        // perform any task
                    }
                    if (swipeDetector.getAction() == Action.BT) {
                        // perform any task
                    }
    
            }
        });
    

    hope you find it helpful... :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: