【问题标题】:GridView onclick not working with on touchGridView onclick 在触摸时不起作用
【发布时间】:2022-01-18 16:45:31
【问题描述】:

我在我的应用程序中实现了一个网格视图。为了刷新网格,我给了它触摸事件。在点击时我对那个网格项目做了一些事情。下面是手势检测器的代码 -

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context ctx) {
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 300;// make it 100 if down
                                                    // swipe is not working
    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;
    }

    // @Override
    // public boolean onSingleTapConfirmed(MotionEvent e) {
    // onClick(); // my method
    // return super.onSingleTapConfirmed(e);
    // }

}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}

// public void onClick() {
// }

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return gestureDetector.onTouchEvent(event);
}
}

下面是网格视图上的触摸和点击监听器 -

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                        }
    });

 gridView.setOnTouchListener(new OnSwipeTouchListener(
            MidnightMainActivity.this) {
        public void onSwipeBottom() {
            gridView.startAnimation(slidedown);
            inflateHomeLayout(MODE_REFRESH);

        }

    });

现在触摸正在工作,但点击时会检测到另一个项目而不是点击一个。我怎么解决这个问题? 提前致谢

【问题讨论】:

  • 为什么在 onDown 函数中返回真值?你确定你处理这个事件?尝试返回 false。
  • 谢谢,现在可以使用了 :)
  • 太好了,我已添加以上评论作为对您问题的回答。

标签: android android-gridview android-gesture


【解决方案1】:

为了尊重 StackOverflow 问题 - 答案流程,我将我的答案放在这里:

为什么在 onDown 函数中返回真值?你确定你处理这个事件?尝试返回false。

【讨论】:

    【解决方案2】:

    onDown 函数返回 false,它可能正在工作。

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

    【讨论】:

    • 似乎与其他答案重复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2016-02-28
    相关资源
    最近更新 更多