我有一个具有ViewPager的活动,每个页面都是具有Zoomable imageView的片段。我已经在可缩放imageView的父视图上使用onTouch方法实现了拖动关闭功能,并且拖动工作正常。但是我想检查在dispatchTouchEvent中是否检测到2位数字,然后忽略onTouch方法并将触摸传递给子视图,以便我可以缩放图像。我试着按照下面的方式编写一个类,但是它不起作用。我该如何解决该问题?因此,缩放和拖动不会相互冲突。
谢谢
CustomView:
public class DraggableRelativeLayout extends RelativeLayout {
private final int THRESHOLD = ScreenUtils.dpToPx(10);
float diffX, diffY;
private float initialXValue;
private float initialYValue;
public DraggableRelativeLayout(Context context) {
super(context);
}
public DraggableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DraggableRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public DraggableRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
initialXValue = ev.getX();
initialYValue = ev.getY();
// return false;
break;
case MotionEvent.ACTION_MOVE:
diffX = ev.getX() - initialXValue;
diffY = ev.getY() - initialYValue;
if (Math.abs(diffY) <THRESHOLD || ev.getPointerCount() > 1)
return true;
else
return super.dispatchTouchEvent(ev);
//break;
case MotionEvent.ACTION_UP:
break;
}
return super.dispatchTouchEvent(ev);
}
}
onTouchListener:
public class PostDetailTouchListener implements View.OnTouchListener {
public PostDetailTouchListener(Context context, View view, View viewToZoom, OnDragInteractionListener mListener) {
dragView = view;
dragListener = mListener;
zoomView = viewToZoom;
}
@Override
public boolean onTouch(View view2, MotionEvent event) {
// doing my work
}
答案 0 :(得分:0)
您可以使用手势检测器来获取详细信息link
class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,float velocityX, float velocityY) {
return true;
}
}