简体   繁体   中英

Android MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP

im trying to implement swipe for my list view item, i have hidden element (like a button ) i want swipe left/right item for showing this button. Everything work perfect. Except i cant click normally on item or button. Because event MotionEvent.ACTION_UP always triggered. Log when i just make simple click/touch on item

 Action DOWN
 OLD X = -329.0
 DX = -329.0
 MOVE RIGHT
 OLD X = -329.0
 DX = -330.5
 MOVE RIGHT
 OLD X = -329.0
 DX = -330.95996
 MOVE RIGHT
 Action UP

Can any help me ? For click on items im trying to use this code

float cx = event.getX();
  if(cx<width && cx>(width-150)) {

and my code

swipeLayout.setOnTouchListener(new View.OnTouchListener() {
            private boolean canClick = false;


            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x1 = event.getX();
                        dX = view.getX() - event.getRawX();
                        dY = view.getY() - event.getRawY();
                        Logger.e("Action DOWN");
                        oldX = dX;
                        return true;
                    case MotionEvent.ACTION_MOVE:

                        Logger.e("OLD X = " + oldX);
                        Logger.e("DX = " + dX);
                        dX = view.getX() - event.getRawX();
                        if (oldX < dX) {
                            Logger.e("MOVE LEFT");
                            view.animate().x(-100).setDuration(50).start();
                            break;
                        }

                        if (oldX > dX) {
                            Logger.e("MOVE RIGHT");
                            view.animate().x(0).setDuration(50).start();
                            break;
                        }


                    case MotionEvent.ACTION_UP:
                        Logger.e("Action UP");
                }
                return false;
            }
        });

Consider using GestureDetector to handle swiping. Here is the possible example:

new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                // Here you handle short quick swipe gesture
                return super.onFling(e1, e2, velocityX, velocityY);
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                    float distanceY) {
                // Here you handle slow finger movement across the screen
                return super.onScroll(e1, e2, distanceX, distanceY);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM