我想在多个文本视图上实现滑动功能,每个视图都实现一个 onclick 监听器。我的问题是,如果我将 onTouch 添加到每个文本视图中,并且由于它们又小又多,它们将无法识别两者的差异。我试图将 ontouch 添加到父布局中,但似乎因为它有文本视图,所以它没有检测到在它们上滑动。
任何人都知道一种在文本视图上实现左右滑动并仍然保留这些 onClick 的好方法?
提前致谢。
我想在多个文本视图上实现滑动功能,每个视图都实现一个 onclick 监听器。我的问题是,如果我将 onTouch 添加到每个文本视图中,并且由于它们又小又多,它们将无法识别两者的差异。我试图将 ontouch 添加到父布局中,但似乎因为它有文本视图,所以它没有检测到在它们上滑动。
任何人都知道一种在文本视图上实现左右滑动并仍然保留这些 onClick 的好方法?
提前致谢。
为了给你一个想法,我有一个 gridview (可能是 any 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;
}
}