Horizontal ProgressBar: Toggling between determinate and indeterminate

I'm trying to toggle a SeekBar between the the two modes to display the state of streaming media.

I've established that the correct graphics show when defining the android:indeterminate tag in XML:

<SeekBar
    android:id="@+id/seekBar1"
    android:indeterminate="false"
    android:progressDrawable="@android:drawable/progress_horizontal"
    android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
    android:indeterminateBehavior="cycle"
    android:indeterminateOnly="false"
    android:progress="33"
    android:secondaryProgress="66"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"></SeekBar>

The trouble is, when trying to switch by calling setIndeterminate(true) , the drawables don't appear to change properly. In the case of indeterminate->determinate, the animation stops, and from determinate->indeterminate, nothing happens.

What am I doing wrong?


The SeekBar onSizeChanged() initializes only the current selected background drawable, and it is called once when the SeekBar is initialized. So a fix might be:

public class CorrectedSeekBar extends SeekBar {

    public CorrectedSeekBar(Context context) {
         super(context);
    }

    public CorrectedSeekBar(Context context, AttributeSet attrs) {
         super(context, attrs);
    }

    public CorrectedSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        boolean current = isIndeterminate();
        setIndeterminate(!current);
        super.onSizeChanged(w, h, oldw, oldh);
        setIndeterminate(current);
        super.onSizeChanged(w, h, oldw, oldh);
    }

}

And in the layout xml use <yourpackage.CorrectedSeekBar ...

This doesnt look nice, other workarounds might be possible but I think this is the main problem (bug?).


I have had the same problem and I solved it by calling postInvalidate() to force the view refresh. I'm on Android 2.3.4.

// Configure the SeekBar
seekBar.setIndeterminate(true);
seekBar.postInvalidate();

Well instead setting the pd to indeterminate just switch its background. if you want drawable animations then i suggest to look into the TransitionDrawable here: TransitionDrawable. so the use of it is to actually add 2 different drawables and animate their transition. i think that should work for your case. but i don't have experience doing something like this on a progress dialog so i cant really tell.

链接地址: http://www.djcxy.com/p/8318.html

上一篇: 在登录设计后更新属性

下一篇: 水平ProgressBar:在确定和不确定之间切换