我想在多个textview上复制滑动功能,其中每个视图都实现了一个onclick Listener。我的问题是,如果我将onTouch添加到每个textview,因为它们很小很多,他们将无法识别两者中的差异。我试图将ontouch添加到父布局,但似乎因为它有文本视图,所以它不会检测到它们的滑动。
任何人都知道在文本视图上左右滑动并保留onClick的好方法吗?
提前致谢。
答案 0 :(得分:0)
为了给你一个想法,我有一个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;
}
}