android - 如何在做一些工作时在 View 上显示 ProgressBar?

标签 android android-listview progress-bar

我有一个包含 EditTextButtonListView 的 View 。 Button的onClick()解析了一些站点(这部分工作正常),但是在ListView中显示解析的信息需要一些时间,所以我想显示小的 ProgressBar 放在 ListView 应该在一段时间后出现的地方。

所以,我将它添加到我的布局中(我在这里写了重要的部分):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout1"
        android:weightSum="1.0">

        <EditText
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight=".86" />

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0px"
            android:layout_weight=".14" />

    </LinearLayout>

    <RelativeLayout 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/layout1"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:id="@+id/progressbar" >

        <ProgressBar 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            style="@android:style/Widget.ProgressBar.Small" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout1">

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>

</RelativeLayout>

在源代码中:

progressBar = (RelativeLayout) findViewById(R.id.progressbar);

每次我想显示 ProgressBar 时,我都会这样做:

progressBar.setVisibility(View.VISIBLE);

用于禁用:

progressBar.setVisibility(View.INVISIBLE);

但这行不通。

我该如何解决这个问题?

最佳答案

无需操纵您的 ProgressBar 的可见性。 ListView 具有称为“空 View ”的功能,仅当您的 ListView 为空时才会显示。以下是您需要执行的操作:

  1. 如果您的 Activity 扩展 ListActivity,请使用 android:id="@android:id/list" 用于您的 ListViewandroid:id="@android:id/empty" 用于您的 ProgressBar
  2. 如果您不扩展 ListActivity,则有一个 setEmptyView() ListView 的方法,它可以让您做同样的事情。

如果您不需要清空 ListView(例如,数据仅在 Activity 启动时加载一次),则上述方法最适合。如果之后需要设置数据,最好使用 ViewSwitcher,它也有很大的优势 - 它可以让你应用过渡动画。有关 ViewSwitcher 的详细信息,请查看 this .

关于android - 如何在做一些工作时在 View 上显示 ProgressBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709992/

相关文章:

Android Studio IDE异常打不开Layout Resources xml文件

android - 清除适配器然后添加项目,修复错误

c# - FtpWebRequest FTP 下载与 ProgressBar

android - 在 Android 屏幕上显示加载叠加层

android - 如何在 QuickBlox 中为 curl 获取 client_identification_sequence

java - 如何将Android源代码组织到文件夹中

android - 单击后保持列表项的颜色

android - 如何以编程方式突出显示 ListView 的项目?

css - modal bootstrap z-index 问题中 css 中的进度条

java - Android 上更详细的 ListView ?