سؤال

I have a SeekBar with an instance of OnSeekBarChangeListener attached. Each time a user changes the value, it's method onProgressChanged() gets fired. That's fine.

But then I'm trying to update the progress of the SeekBar like this:

seekBar.setProgress(value);

I expect that no events will fire - just SeekBar's progress will change. But onProgressChanged() gets called in this case also.

How can I change the progress of a SeekBar without firing events?

هل كانت مفيدة؟

المحلول

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

Look at the parameters. You have a boolean fromUser, use it.

if (fromUser) {
.........
.........
}

Doing so code will be execute only when change was initiated by user. If you set seekbar's value from code the event will fire but code won't execute due to the if() statement. Bye. -Marco-

نصائح أخرى

You're changing the progress on the SeekBar. Of course onProgressChanged will be called.

If you'd like to avoid this you can null out the listener before setting your value

seekBar.setOnSeekBarChangeListener(null);
seekBar.setProgress(value);
seekBar.setOnSeekBarChangeListener(this);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top