在android列表项中捕获触摸事件并防止滚动
我在这里尝试一个疯狂的想法,在某个列表视图的项中放置一个自定义控件。只有当用户触到某个触发点,然后他们可以“拖动”时,控件才会被“激活” 我的问题是,在onTouchEvent(…)中我可以做什么来防止listview接收事件和滚动。现在我可以触摸并控制控件,但是如果我的手指上下移动太多,listview就会接管并开始滚动,那么我的视图甚至不会收到动作事件 以下是我当前的onTouchEvent代码:在android列表项中捕获触摸事件并防止滚动,android,events,custom-controls,touch,android-event,Android,Events,Custom Controls,Touch,Android Event,我在这里尝试一个疯狂的想法,在某个列表视图的项中放置一个自定义控件。只有当用户触到某个触发点,然后他们可以“拖动”时,控件才会被“激活” 我的问题是,在onTouchEvent(…)中我可以做什么来防止listview接收事件和滚动。现在我可以触摸并控制控件,但是如果我的手指上下移动太多,listview就会接管并开始滚动,那么我的视图甚至不会收到动作事件 以下是我当前的onTouchEvent代码: if (e.getAction() == MotionEvent.ACTION_DOWN) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("SwipeView", "onTouchEvent - ACTION_DOWN" + e.getX() + " " + e.getY());
int midX = (int)(this.getWidth() / 2);
int midY = (int)(this.getHeight() / 2);
if (Math.abs(e.getX() - midX) < 100 &&
Math.abs(e.getY() - midY) < 100) {
Log.d("SwipeView", "HEY");
setDragActive(true);
}
this.invalidate();
return true;
} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
_current[0] = e.getX();
_current[1] = e.getY();
this.invalidate();
return true;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
_current[0] = 0;
_current[1] = 0;
setDragActive(false);
this.invalidate();
return true;
}
if(e.getAction()==MotionEvent.ACTION\u DOWN){
Log.d(“SwipeView”、“onTouchEvent-ACTION_DOWN”+e.getX()+“”+e.getY());
int midX=(int)(this.getWidth()/2);
int midY=(int)(this.getHeight()/2);
if(Math.abs(e.getX()-midX)<100&&
Math.abs(e.getY()-midY)<100){
Log.d(“SwipeView”、“HEY”);
setDragActive(真);
}
这个。使无效();
返回true;
}else if(e.getAction()==MotionEvent.ACTION\u MOVE){
_当前[0]=e.getX();
_当前[1]=e.getY();
这个。使无效();
返回true;
}else if(e.getAction()==MotionEvent.ACTION\u UP){
_电流[0]=0;
_电流[1]=0;
setDragActive(假);
这个。使无效();
返回true;
}
我确信它在某种程度上与事件层次结构有关。这可能不是您想要的,但可以在您的活动中实现捕获功能。加
private View capturedView;
public void setCapturedView(View view) { this.capturedView = view); }
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return (this.capturedView != null) ?
this.capturedView.dispatchTouchEvent(event) :
super.dispatchTouchEvent(event);
}
对于您的活动,只需在
ACTION\u向下
上传递视图,在ACTION\u向上
上传递空值即可。虽然不太好看,但很管用。我相信有一种正确的方法可以做到这一点。终于学会了正确的方法: