我在我的应用程序中实现了一个网格视图。为了刷新网格我已经给它了触摸事件。然后点击我用网格项做一些事情。下面是手势检测器的代码 -
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);
}
});
现在触摸正在工作但点击检测到另一个项目而不是点击一个项目。我怎么解决这个问题? 提前致谢
答案 0 :(得分:3)
为了尊重StackOverflow问题 - 答案流程,我把答案放在这里:
为什么在onDown函数中返回true值?你确定要处理这个事件吗?尝试返回false。