onStopTrackingTouch事件被触发两次,这是一个问题,因为我只需要创建一个getRequest。这是我的代码:
VerticalSeekbar.java
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;
}
Information.java
onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
radio = (progress / (double) 10);
tv_radio.setText(radio + " Km");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){
Log.i("vertical", "onStart");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar){
Log.i("vertical", "onStopTracking"); //printed twice
getRequest request = new getRequest();
request.execute("http://...");
}
};
sb_radio.setOnSeekBarChangeListener(onSeekBarChangeListener);
知道为什么MotionEvent.ACTION_UP
被检测到两次?即使我只是在Seekbar
中进行简单的点击而不拖动。
答案 0 :(得分:1)
将为每个MotionEvent.ACTION_DOWN
,MotionEvent.ACTION_UP
和MotionEvent.ACTION_MOVE
所以问题是,在break
之后有一个MotionEvent.ACTION_MOVE
语句丢失,因此即使你处于MotionEvent.ACTION_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;
}