gpt4 book ai didi

android - MotionEvent.ACTION_UP 事件被 seekbar 触发两次

转载 作者:行者123 更新时间:2023-11-30 01:00:58 24 4
gpt4 key购买 nike

onStopTrackingTouch 事件被触发了两次,这是一个问题,因为我只需要发出一个 getRequest。这是我的代码:

垂直搜索栏.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;
}

信息.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 中简单单击而不拖动。

最佳答案

每个 MotionEvent.ACTION_DOWNMotionEvent.ACTION_UPMotionEvent.ACTION_MOVE 都会调用 TouchListener

所以问题是,在 MotionEvent.ACTION_MOVE 之后缺少一个 break 语句,所以即使您属于 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;
}

关于android - MotionEvent.ACTION_UP 事件被 seekbar 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com