Android MotionEvent.ACTION_MOVE和MotionEvent.ACTION_UP

时间:2017-08-03 11:42:02

标签: java android listview swipe motionevent

我试图为我的列表视图项实现滑动,我有隐藏元素(如按钮) 我想要向左/向右滑动项目以显示此按钮。一切都很完美。除了我不能正常点击项目或按钮。因为事件MotionEvent.ACTION_UP总是被触发。 当我只是简单点击/触摸项目

时记录
 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

能帮到我吗? 点击试图使用此代码的项目

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

和我的代码

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

1 个答案:

答案 0 :(得分:0)

考虑使用GestureDetector来处理滑动。 以下是可能的示例:

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