ContentLoadingProgressBar 永远不会显示

22

我已经将我的片段中简单的 ProgressBar 替换为 ContentLoadingProgressBar。 现在片段布局看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            style="@style/simple_match_parent_style">

<android.support.v4.widget.ContentLoadingProgressBar
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>

<TextView
    android:id="@+id/txt_no_songs"
    android:text="@string/txt_no_songs"
    android:layout_centerInParent="true"
    style="@style/txt_no_songs"/>

<ListView
    android:id="@+id/list"
    android:fastScrollEnabled="true"
    android:divider="@null"
    style="@style/simple_match_parent_style"/>

</RelativeLayout>

我在 onViewCreated 中展示了这个进度条:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    progressBar = (ContentLoadingProgressBar) view.findViewById(R.id.progress);
    progressBar.show();
    ...
}

为了测试目的,我已经删除了progressBar.hide()的调用,以使进度条在任何情况下都能显示。但问题是我的ContentLoadingProgressBar从未真正显示。

我应该做些什么来显示ContentLoadingProgressBar吗?我没有找到任何相关使用ContentLoadingProgressBar的例子,因此希望提供有用的示例。提前感谢。


也许您可以在此链接中找到您正在寻找的内容:https://dev59.com/KmIj5IYBdhLWcg3wRTP3 - Maheera Jazi
我看到了这个话题,但是我在那里没有找到任何有用的东西。 - Bersh
你不能在这里使用progressBar.show(); 在onViewCreated()调用之后,会调用ContentLoadingProgressBar.onAttachedToWindow()查看源代码,他们在这里删除了show回调。所以,进度条从未被显示。 - tarn
5个回答

31

看起来你需要一个样式,例如...

style="?android:attr/android:progressBarStyleHorizontal"

如果我这样做,它对我有效,如果不这样做,我就什么也看不见。

另外考虑将它放在内容上面(即在xml下面)。


9
如果你将代码改为style="?android:attr/progressBarStyleHorizontal",它就可以工作,否则会出现以下编译错误:AAPT: error: resource android:attr/android:progressBarStyleHorizontal not found. - Luan Barbosa
现在您还可以使用更新的样式:style="@style/Widget.AppCompat.ProgressBar.Horizontal"。 - ConcernedHobbit

10
我尝试过这个:
<android.support.v4.widget.ContentLoadingProgressBar
        android:id="@+id/address_looking_up"
        style="?android:attr/android:progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="visible" />

但是,在Android 5.0上它不起作用。ContentLoadingProgressbar的更多详细信息应该在这里。

附注:好了,这里“style”的属性很重要。把样式改为 "?android:attr/progressBarStyleLarge" 就可以工作了。

同时,此小部件的显示效果取决于您应用程序的主题。


2
你应该给ContentLoadingProgressBar添加样式。
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"/>

0

1.SDK表示:“在等待最短延迟后显示进度视图。如果在此期间调用了hide(),则该视图将永远不会可见。” 2. 我认为您的布局应该修改为这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/simple_match_parent_style">
<TextView
android:id="@+id/txt_no_songs"
android:text="@string/txt_no_songs"
android:layout_centerInParent="true"
style="@style/txt_no_songs"/>

<ListView
android:id="@+id/list"
android:fastScrollEnabled="true"
android:divider="@null"
style="@style/simple_match_parent_style"/>
<android.support.v4.widget.ContentLoadingProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
</RelativeLayout>

0

如果您同时创建多个视图,则请检查ContentLoadingProgressBar.onAttachedToWindow()ContentLoadingProgressBar.onDetachedToWindow(),这可能会导致在onAttachedToWindow()onDetachedToWindow()中调用private removeCallBacks()。从而导致可运行项无法运行,这意味着不会显示任何内容。

例如,您可以: 简单地删除onAttachedToWindow()onDetachedToWindow()中的private removeCallBacks()调用,以防止发生这种情况。不要忘记将private removeCallBacks()更改为public并在onDestroyView()中调用它以进行适当的清理。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接