Changing step values in seekbar?

前端 未结 8 1942
耶瑟儿~
耶瑟儿~ 2020-12-12 16:32

I have a seekbar, while moving it I want to change values from 0 to 200. I have a TextView, where I display those values while moving the seekbar. But I don\'t want to have

相关标签:
8条回答
  • 2020-12-12 16:45

    The easieset way I can think of, is simply defining:

    SeekBar yourSeekBar = (SeekBar)findViewById(R.id.yourSeekBarId);
    yourSeekbar.setMax(20);
    

    Next, override those methods (the empty methods are also required, even if they are empty):

    yourSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {          
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    if (progress >= 0 && progress <= sizeSeekBar.getMax()) {                        
    
                        String progressString = String.valueOf(progress * 10);
                        yourTextView.setText(progressString); // the TextView Reference
                        seekBar.setSecondaryProgress(progress);
                    }
                }
    
            }
        });
    

    The idea is to only define 20 values for the seekbar, but always multiply the value by 10 and display that value. If you do not really need 200 values, then there is no point in using 200 values.

    0 讨论(0)
  • 2020-12-12 16:46

    Use the Slider in Material Components Library using the android:stepSize attribute:

        <com.google.android.material.slider.Slider
            android:valueFrom="0"
            android:valueTo="200"
            android:stepSize="10"
           ..>
    

    0 讨论(0)
  • 2020-12-12 16:54

    use Math.round function

    fun round(n: Int): Int {
        // Smaller multiple
        val a = n / 10 * 10
        // Larger multiple
        val b = a + 10
        // Return of closest of two
        return if (n - a > b - n) b else a
    }
    
    0 讨论(0)
  • 2020-12-12 16:56

    You can also achieve this by defining custom SeekBar class as below

    public class CustomSeekBar extends AppCompatSeekBar {
    
    
    int SEEKBAR_MULTIPLIER = 1;
    
    public CustomSeekBar(Context context) {
        super(context);
    }
    
    public CustomSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    public void setSeekBarConfig( int SEEKBAR_MULTIPLIER, int SEEKBAR_MAX){
        setMax(SEEKBAR_MAX);
        this.SEEKBAR_MULTIPLIER = SEEKBAR_MULTIPLIER;
    }
    
    @Override
    public int getProgress() {
        return super.getProgress() * SEEKBAR_MULTIPLIER;
    }
    }
    

    And can use by following manner

     CustomSeekBar mCustomSeekBar = (CustomSeekBar) findViewById(R.id.customSeekBar);
     mSeekBar.setSeekBarConfig(10,20);
    

    In xml you should add CustomSeekBar instead of SeekBar

    <*.CustomSeekBar
        com.quemb.qmbform.view:setSeekBarConfig="10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar" />
    

    So that you can reuse the custom seekBar in entire App with out repeating whole extra calculation.

    0 讨论(0)
  • 2020-12-12 17:01

    You should use the SeekBar.OnSeekBarChangeListener for tracking the progress change. Call mseek.setMax(200). Do a modulo operation on the current value to decide if the textview should be updated or not.

    SeekBar.OnSeekBarChangeListener() {
    
        void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (progress % 10 == 0) {
                textview.setText(""+progress);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 17:02

    for the minimum issue, you can set an offset on your progress. This way, the user can't go under your minimal value.

    int offset = 10;
    
    seekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
    seekBar.setProgress(0);
    seekBar.setMax(190); //with the offset, you need to adapt the max value
    TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
    seekBarValue.setText(""+(seekBar.getProgress() + offset));
    
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
    
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress += offset;
    
            seekBarValue.setText(String.valueOf(progress));
        }
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
    
        }
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题