Custom inline SeekBarPreference - how to set SeekBar progress on the 1st run?

纵然是瞬间 提交于 2019-12-05 09:32:08

You need to override onGetDefaultValue in SeekBarPreference.

Try this:

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getInt(index, 0);
}

When you instantiate

mSeek1 = (SeekBarPreference) findPreference(SEEK_1);

private int mProgress; is 0

So probably it will help if you add a setter in the SeekBarPreference for mProgress and set its value:

mSeek1 = (SeekBarPreference) findPreference(SEEK_1);
mSeek1.setMProgress(valueIhopeIsKnownAtThisMonent);

SOLUTION: It's a Bug in ProgressBar!

finally... I think I found the solution...

this does not work as one would expect:

seekBar.setMax(50);
seekBar.setProgress(20);
seekBar.setMax(20);
seekBar.setProgress(20);

The setProgress(...) seems to not trigger the update on the drawable if the same value is passed again. But it's not triggered during the setMax, too. So the update is missing. Seems like a Bug in the android ProgressBar! This took me about 8 hours now.. lol :D

To solve this, I'm just doing a bar.setProgress(0) before each update... this is only a workaround, but it works for me as expected:

seekBar.setMax(50);
seekBar.setProgress(20);
seekBar.setProgress(0); // <--
seekBar.setMax(20);
seekBar.setProgress(20);

Add this code before you call mSeekBar.setProgress(mProgress); :

String preferenceSummary = this.getSummary().toString();
preferenceSummary = preferenceSummary.replace("$ ", "");
mProgress = Integer.valueOf(preferenceSummary);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!