给你一个主意,我有一个gridview(可以是任何
Layout
)在有许多单元格的情况下,要检测对所有这些单元格的滑动,还要检测对每个单元格的单击,我有以下代码,当然您需要根据需要进行自定义:
private GestureDetectorCompat gestureDetector;
protected void onAttachments() {
gestureDetector = new GestureDetectorCompat(this.context, new SingleTapConfirm());
//get the width and height of the grid view
final int cellWidth = calendarGridView.getWidth() / 7;
final int cellHeight = calendarGridView.getHeight() / 5;
calendarGridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent touchEvent) {
int touchX = (int) touchEvent.getX();
int touchY = (int) touchEvent.getY();
if(touchEvent.getAction() == MotionEvent.ACTION_MOVE || gestureDetector.onTouchEvent(touchEvent)){
if (touchX <= calendarGridView.getWidth() && touchY <= calendarGridView.getHeight()) {
int cellX = (touchX / cellWidth);
int cellY = (touchY / cellHeight);
touchPosition = cellX + (7 * (cellY));
positionPrinter.setText("child: " + cellX + "," + cellY);
cellIDprinter.setText(Integer.toString(touchPosition));
// Do you stuff
... ... ...
}
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector != null)
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
return true;
}
}