【问题标题】:Android: How to combine Swipe gesture with Longpress in a item on listViewAndroid:如何在listView上的项目中结合滑动手势和长按
【发布时间】:2013-01-09 23:51:18
【问题描述】:

我有一个我个性化的列表视图,我添加了setOnItemLongClickListener(),效果很好。然后我决定实现一个手势listView.setOnTouchListener(new OnSwipeTouchListener()),它也很好用。我从另一个帖子中复制了 OnSwipetouchListener 类。

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

我想做什么:

滑动侦听器在 2 秒以下获取所有内容,之后所有内容都进入 longpress。所以我仍然可以通过滑动手势更改列表视图的内容,还可以为每个列表项创建函数。

我的代码:

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...
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      相关资源
      最近更新 更多