简体   繁体   English

试图在android seekbar上实现max的动态增加

[英]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. 我希望搜索栏以最大100开始,然后如果用户将搜索栏一直移动到100,则最大值上升到250,如果用户将搜索栏一直移动到250,则它会上升到500,如果用户将搜寻栏一直移动到500,则最终停止在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. 问题在于,当用户将搜索栏移动到最大值时,由于用户将手指放在那里,因此android认为它始终处于最大值,因此它只是立即增加到500+,所以我需要找到一种方法来禁用增加最大值后触摸,直到用户抬起手指。

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. 所以...我想要从搜索栏中断用户的触摸,以便它等到下一个NEW触摸事件,这样就不会发生这种情况。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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