Seekbar无法正常工作

时间:2013-05-27 04:03:10

标签: java android seekbar

我希望每次触摸它时都能从我的应用程序中获取搜索栏的价值。我在搜索栏中声明的值将添加到我的网址中,但每次触摸搜索栏时,它都会给我一个循环,并且网址会在progressChanged上作为数量搜索栏进行循环和加载。这是我的代码:

int period = 0;
SeekTime.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
        // TODO Auto-generated method stub
        if (SeekTime.getProgress() <= 3){
            SeekTime.setProgress(0);
            period=7;
        }
        if (SeekTime.getProgress() > 3 && SeekTime.getProgress() <= 8) {
            SeekTime.setProgress(5);
            period =30;
        }
        if (SeekTime.getProgress() >8 && SeekTime.getProgress()<=13){
            SeekTime.setProgress(10);
            period = 90;
        }
        if (SeekTime.getProgress() >13 && SeekTime.getProgress()<=18){
            SeekTime.setProgress(15);
            period = 180;
        }
        if (SeekTime.getProgress() >18 && SeekTime.getProgress()<=23){
            SeekTime.setProgress(20);
            period = 270;
        }
        if (SeekTime.getProgress() >23){
            SeekTime.setProgress(25);
            period = 360;
        }
        new LoadData(URL+period).execute();

1 个答案:

答案 0 :(得分:1)

我认为你得到的问题是因为当你调用SeekTime.setProgress()时正在改变进度 - 它再次调用onProgressChange()。

您应该做的是检查用户是否已完成进度更改。你有fromUser变量,允许你控制它。

int period = 0;
SeekTime.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
    // TODO Auto-generated method stub
if(fromUser) {
    if (SeekTime.getProgress() <= 3){
        SeekTime.setProgress(0);
        period=7;
    }
    if (SeekTime.getProgress() > 3 && SeekTime.getProgress() <= 8) {
        SeekTime.setProgress(5);
        period =30;
    }
    if (SeekTime.getProgress() >8 && SeekTime.getProgress()<=13){
        SeekTime.setProgress(10);
        period = 90;
    }
    if (SeekTime.getProgress() >13 && SeekTime.getProgress()<=18){
        SeekTime.setProgress(15);
        period = 180;
    }
    if (SeekTime.getProgress() >18 && SeekTime.getProgress()<=23){
        SeekTime.setProgress(20);
        period = 270;
    }
    if (SeekTime.getProgress() >23){
        SeekTime.setProgress(25);
        period = 360;
    }
    new LoadData(URL+period).execute();
}
}