简体   繁体   中英

trying to implement dynamic increase of max on android seekbar

I'm implementing a seekbar that allows users to set a price. I want the seekbar to start out with a max of 100, then if the user moves the seekbar all the way up to 100, the max goes up to 250 and if the user moves the seekbar all the way up to 250 it goes up to 500, finally stopping at 500+ if the user moves the seekbar all the way up to 500.

The code that I've written works, for the most part. The problem is that when the user moves the seekbar up to the max, since the user is holding their finger there, android thinks that it's constantly at the max so it just instantly increases to 500+, so I need to find a way to disable touch after increasing the max until the user lifts their finger.

So...I want to break the user's touch from the seekbar so that it waits until the next NEW touch event so that this won't happen. If anyone knows a way to do this, please let me know asap!! thanks!

What I've already tried is disable and reanabling the seekbar when it reaches the max, in hopes of breaking the touch, but this doesn't work.

The code I have is below:

private class PriceChangeListener implements SeekBar.OnSeekBarChangeListener{

    private TextView priceView = (TextView) findViewById(R.id.input_price_label);
    private TextView maxPriceView = (TextView) findViewById(R.id.input_price_max_label);
    private boolean increaseMax;

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        String textToSet = "About how much will it cost? $" + String.valueOf(progress);
        if(progress < 500)
            priceView.setText(textToSet);
        else
            priceView.setText(textToSet + "+");


        if(progress == seekBar.getMax() && increaseMax) {
            seekBar.setEnabled(false);
            switch (progress) {
                case 100:
                    seekBar.setMax(250);
                    increaseMax = false;
                    maxPriceView.setText("$" + String.valueOf(seekBar.getMax()));
                    break;
                case 250:
                    seekBar.setMax(500);
                    increaseMax = false;
                    maxPriceView.setText("$" + String.valueOf(seekBar.getMax()));
                    break;
                case 500:
                    maxPriceView.setText("$500+");
                    break;
            }
            seekBar.setEnabled(true);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        increaseMax = true;
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // Do nothing
    }
}

Further improving my comment, give this a shot

private boolean newTouch = false;
private int oldProgress;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
    if (newTouch) {
        oldProgress = progress;
        priceView.setText("About how much will it cost? $" + String.valueOf(progress));
        if(progress == seekBar.getMax()) {
            newTouch = false;
            switch (progress) {
                case 100:
                    seekBar.setMax(250);
                    seekBar.setProgress(100);  
                    maxPriceView.setText("$" + String.valueOf(seekBar.getMax()));
                    break;
                case 250:
                    seekBar.setMax(500);
                    seekBar.setProgress(250);
                    maxPriceView.setText("$" + String.valueOf(seekBar.getMax()));
                    break;
                case 500:
                    seekBar.setProgress(250);
                    maxPriceView.setText("$500+");
                    break;
            }
        }
    } else {
        seekBar.setProgress(oldProgress);
    }
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    newTouch = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM