Android MotionEvent.ACTION_UP事件使用seekbar触发两次
onStopTrackingTouch事件被触发两次,这是一个问题,因为我只需要发出一个getRequest。这是我的密码: VerticalSeekbar.javaAndroid MotionEvent.ACTION_UP事件使用seekbar触发两次,android,motionevent,android-vertical-seekbar,Android,Motionevent,Android Vertical Seekbar,onStopTrackingTouch事件被触发两次,这是一个问题,因为我只需要发出一个getRequest。这是我的密码: VerticalSeekbar.java public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN:
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
super.onTouchEvent(event);
int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
// Ensure progress stays within boundaries
if(progress < 0) {progress = 0;}
if(progress > getMax()) {
progress = getMax();}
setProgress(progress); // Draw progress
if(progress != lastProgress) {
// Only enact listener if the progress has actually changed
lastProgress = progress;
Information.onSeekBarChangeListener.onProgressChanged(this, progress, true);
}
onSizeChanged(getWidth(), getHeight() , 0, 0);
Log.i("radio", "action move");
setPressed(true);
setSelected(true);
case MotionEvent.ACTION_UP:
Log.i("radio", "action up"); //printed twice
Information.onSeekBarChangeListener.onStopTrackingTouch(this);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
知道为什么会检测到两次
MotionEvent.ACTION\u UP
?即使我只需在Seekbar
中进行简单的点击,而无需拖动。TouchListener将为每个运动事件调用。ACTION\u DOWN
,运动事件.ACTION\u UP
,以及运动事件.ACTION\u MOVE
所以问题是,在MotionEvent之后缺少break
语句。ACTION\u MOVE
因此,即使在MotionEvent.ACTION\u MOVE
情况下,控件仍将继续执行下一个情况,直到到达某个停止点,如break
等
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
super.onTouchEvent(event);
int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
if(progress < 0) {progress = 0;}
if(progress > getMax()) {
progress = getMax();}
setProgress(progress);
if(progress != lastProgress) {
lastProgress = progress;
Information.onSeekBarChangeListener.onProgressChanged(this, progress, true);
}
onSizeChanged(getWidth(), getHeight() , 0, 0);
Log.i("radio", "action move");
setPressed(true);
setSelected(true);
break; // add a break statement here and problem solved
case MotionEvent.ACTION_UP:
Log.i("radio", "action up");
Information.onSeekBarChangeListener.onStopTrackingTouch(this);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
case MotionEvent.ACTION\u移动:
超级事件(事件);
int progress=getMax()-(int)(getMax()*event.getY()/getHeight());
如果(进度<0){progress=0;}
如果(进度>getMax()){
进度=getMax();}
设定进度(进度);
如果(进度!=上次进度){
lastProgress=进度;
Information.onseekbarchaneglistener.onProgressChanged(this,progress,true);
}
onSizeChanged(getWidth(),getHeight(),0,0);
Log.i(“无线电”,“动作移动”);
setPressed(true);
已选择(正确);
break;//在此处添加break语句并解决问题
case MotionEvent.ACTION\u UP:
Log.i(“无线电”,“行动起来”);
Information.onseekbarchaneglistener.onStopTrackingTouch(此);
打破
case MotionEvent.ACTION\u取消:
打破
}
返回true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
super.onTouchEvent(event);
int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
if(progress < 0) {progress = 0;}
if(progress > getMax()) {
progress = getMax();}
setProgress(progress);
if(progress != lastProgress) {
lastProgress = progress;
Information.onSeekBarChangeListener.onProgressChanged(this, progress, true);
}
onSizeChanged(getWidth(), getHeight() , 0, 0);
Log.i("radio", "action move");
setPressed(true);
setSelected(true);
break; // add a break statement here and problem solved
case MotionEvent.ACTION_UP:
Log.i("radio", "action up");
Information.onSeekBarChangeListener.onStopTrackingTouch(this);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}