开发者

Prevent SeekBar from moving past a dynamic point

开发者 https://www.devze.com 2023-02-05 04:48 出处:网络
I need two SeekBars for setting a minimum and maximum, and the thumb on each SeekBar cannot move past the other. Is there a way to disallow moving the SeekBar pas开发者_如何学编程t a specific point?Fo

I need two SeekBars for setting a minimum and maximum, and the thumb on each SeekBar cannot move past the other. Is there a way to disallow moving the SeekBar pas开发者_如何学编程t a specific point?


Found the solution myself: in SeekBar.OnSeekBarChangeListener.onProgressChanged() method, just set the progress to a correct value, which in this case is the same as the other SeekBar

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
    if (!isLegalMove(seekBar)) {
        seekBar.setProgress(mOtherSeekBar.getProgress());
    } 
}

private boolean isLegalMove(SeekBar thisSeekBar) {
    if (mOtherSeekBar == null) {
        return true;
    }
    return mIsMax && mOtherSeekBar.getProgress() <= thisSeekBar.getProgress() ||
        !mIsMax && mOtherSeekBar.getProgress() >= thisSeekBar.getProgress();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消