【问题标题】:How to add ProgressBar to SherlockListActivity class如何将 ProgressBar 添加到 SherlockListActivity 类
【发布时间】:2012-12-27 20:44:18
【问题描述】:

我有 MainActivity,它扩展了 SherlockListActivity。这里是 onCreate 方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new MyXMLDownloader().execute(this);
}

MyXMLDownloader 只是 AsyncTask 类,我用它从 http 下载 XML 文件。在 onPostExecute 方法中,我调用了 makeMainGUI(),它基于该 XML 文件创建 GUI(我的意思是它填充了我的 ListView)。

这里是 main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我想添加 ProgressBar 小部件(和一些文本)。它应该在下载 XML 之前显示,并且在 ListView 被填充后它应该消失(当然它发生在下载 XML 之后)。我怎样才能做到这一点?

我对此有疑问,因为我必须在 main.xml 中有 ListView,因为我的 MainActivity 类扩展了 SherlockListActivity。

编辑:

我设法做到了这样的事情:

<ProgressBar
        android:id="@android:id/empty"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

我将 ProgressBar 添加到 main.xml 并带有“空”ID。只要 ListView 为空就显示,这是正确的。但我也不知道如何在这里放置一个 TextView,因为只有一个项目可以有一个“空”的 id。

编辑2: 因为我认为我设法做到了这一点。在 main.xml 我添加了以下内容:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" 
    android:id="@android:id/empty">
    <ProgressBar
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" 
    android:id="@+id/progressBar1"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Please wait"
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:id="@+id/waitingTextView"/>

</LinearLayout>

这是我需要的。谢谢!

【问题讨论】:

    标签: android android-layout listview listactivity android-progressbar


    【解决方案1】:

    在 AsyncTask 类中使用 onPrexecute 方法显示进度对话框并使用 onPostExecute 关闭它:

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(YOUR_ACTIVITY_CLASS_NAME.this);
        pDialog.setMessage("Please Wait");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    
    @Override
    protected void onPostExecute(String str)
    {
        // Dismiss the dialog once finished
        pDialog.dismiss();  
    }
    

    在调用它之前不要忘记定义 pDialog:

    ProgresDialog pDialog;
    

    【讨论】:

    • 感谢您的回答。但我真的不想在这里有任何对话框。我希望在加载后将显示我的 ListView 的位置显示 ProgressBar。
    猜你喜欢
    • 2013-07-29
    • 1970-01-01
    • 2020-09-28
    • 2015-11-09
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多