简体   繁体   中英

Android SeekBar strange behaviour

I have a default SeekBar in Android with 3 possible values: 0,1,2 (left, middle, right).

But it takes some value only when finger is exactly on the point(corresponding to the value) or to the right of it. If the finger is even 1 pixel left of the point, seekbar takes previous value.

I want it to take value when finger is near the point. My explanation skill is very poor, so see the picture: 画画

If using 3 points isn't enough, why not add more points and map back to your 3? eg

private static final double RATIOS = new double[]{ .33, 66, 100 }
private static final int FAKED_POINTS = new int[]{ 0, 50, 100 }

private void setupSeekBar() {
    // Your normal setup
    seekBar.setMax(100);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if(!fromUser) // We only want to do this when the user changes the values
        return;

    for(int i = 0; i < RATIOS.length; i++){
        if(progress <= RATIOS[i]){
            onRealProgressChanged(i);

            seekBar.setProgress(FAKED_POINTS[i]); //  This way it always stays on our 3 faked points
        }
    }
}

private void onRealProgressChanged(int realProgress){
    // Deal with the real changed value (being 0, 1, or 2)
}

If you need even more fine-tuning, you can scale that up another order of magnitude.

Not very clean or pretty, but should point you in the right direction.

I worked on a custom Seekbar view library that could help you.

Please use it and if you like it, share it!

Add this line to your gradle file in the app module

compile 'mx.segundamano.doubleseekbarview:seekbars:1.0.2'

and add this to your build.gradle project level file

repositories {
    maven {
        url  "http://dl.bintray.com/segundamanomx/maven" 
    }
}

Inside are two custom view, one with one selector and the other with two selectors.

Enjoy it ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM