سؤال

I'd like to set a max progress value in a Seekbar that is not the same as the max value.

For example max would be 100 and max progress 50, so the progress slider could only be dragged to the middle. Is there a way to do that?

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

المحلول

You can try the following

int seekBarMaxValue = 100;
int seekBarDraggableMaxValue = 50;

seekBar.setMax(seekBarMaxValue );

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangetListener() {
  public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
     seekBar.setProgress(progress > seekBarDraggableMaxValue ? seekBarDraggableMaxValue : progress);
  }
});

Hope it helps.

نصائح أخرى

It seems like you want three numbers only (ex. 0; 50; 100); just pretend each number is represents another number or multiply the progress by the common divisor:

Example

  • 0 = 0 or (0 * 50)
  • 1 = 50 or (1 * 50)
  • 2 = 100 or (2 * 50)

    int int_actual_number = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ...
    seek_sample.setMax(2);
    seek_sample.setProgress(0);
    seek_sample.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int_actual_number = progress * 50;
    
        }
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
    
        }
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
    
        }
    
    });
    

You can use different common divisors and amount of numbers to test this method for your needs.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top