Android:如何在列表视图上的项目中将滑动手势与长按组合

时间:2013-01-09 23:51:18

标签: android android-listview gesture swipe long-press

我有一个我个性化的列表视图,我添加了setOnItemLongClickListener(),效果很好。然后我决定实施一个也很有效的手势listView.setOnTouchListener(new OnSwipeTouchListener())。我从另一篇文章中复制了OnSwipetouchListener课程。

事情是,当我添加滑动侦听器时,longPress不再起作用了。我想这是因为滑动监听器为自己采取了长按操作,并且不允许longPress做任何事情。

我想做什么:

滑动侦听器会在2秒内获得所有内容,之后一切都会变为长按。所以我仍然可以通过滑动手势更改列表视图内容,我还可以为每个列表项创建函数。

我的代码:

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View v, final MotionEvent event) {
        //super.onTouch(v, event);
         return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
...methods...
}

1 个答案:

答案 0 :(得分:1)

删除onDown方法。现在,它始终返回true并阻止处理longPress。

相关问题