水平进度条:在确定和不确定之间切换

发布于 2024-11-18 13:55:19 字数 826 浏览 4 评论 0 原文

我试图在两种模式之间切换 SeekBar 以显示流媒体的状态。

我已经确定,在 XML 中定义 android:independent 标记时会显示正确的图形:

<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>

example

问题是,当尝试通过调用 setIndependent(true) 进行切换时,可绘制对象似乎没有正确更改。在不确定->确定的情况下,动画停止,在确定->不确定的情况下,什么也不会发生。

我做错了什么?

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>

example

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

靑春怀旧 2024-11-25 13:55:19

我遇到了同样的问题,我通过调用 postInvalidate() 强制视图刷新来解决它。
我使用的是 Android 2.3.4。

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

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();
吾家有女初长成 2024-11-25 13:55:19

SeekBar onSizeChanged() 仅初始化当前选定的背景可绘制对象,并在初始化 SeekBar 时调用一次。
所以修复可能是:

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);
    }

}

在布局 xml 中使用

这看起来不太好,可能还有其他解决方法,但我认为这是主要问题(bug?)。

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?).

放血 2024-11-25 13:55:19

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.

淡紫姑娘! 2024-11-25 13:55:19

解决方案实际上非常简单,与启动动画可绘制对象所使用的技巧相同。此问题似乎仅影响 Android 2.3(也可能影响旧版本的 Android)。

每当你想设置:

progressBar.setIndeterminate(true);

使用这部分代码代替:

this.progressBar.post(new Runnable()
{
    @Override
    public void run()
    {
        progressBar.setIndeterminate(true);
    }
});

The solution is actually very simple, the same trick you have to use to start an animated drawable. This issue only seems to affect Android 2.3 (maybe also older versions of Android).

whenever you want to set:

progressBar.setIndeterminate(true);

use this part of code instead:

this.progressBar.post(new Runnable()
{
    @Override
    public void run()
    {
        progressBar.setIndeterminate(true);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文