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

azi*_*ian 6 android android-listview progress-bar

我有一个观点EditText,ButtonListView.按钮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>
Run Code Online (Sandbox Code Playgroud)

在源代码中:

progressBar = (RelativeLayout) findViewById(R.id.progressbar);
Run Code Online (Sandbox Code Playgroud)

每次我想表明ProgressBar,我这样做:

progressBar.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

禁用:

progressBar.setVisibility(View.INVISIBLE);
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

我该如何解决这个问题?

And*_*nko 2

无需操纵您的ProgressBar可见性。ListView有一个称为“空视图”的功能,仅当您的视图ListView为空时才会显示。使用它需要执行以下操作:

  1. 如果您的活动延长ListActivity,请使用 android:id="@android:id/list"for yourListViewandroid:id="@android:id/empty"for your ProgressBar
  2. 如果你不扩展, 你的方法ListActivity有一个,它可以让你做同样的事情。setEmptyView()ListView

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