如何在执行某些操作时在视图上显示进度条?

6
我有一个包含EditTextButtonListView的视图。 当点击

但这样做不起作用。这并不是很有帮助。能否更好地解释一下问题是什么? - user
我看不到任何ProgressBar显示。它的工作方式就像没有添加ProgressBar一样。 - azizbekian
2
同意Luksprog的观点,但我猜测你的解析是阻塞的(因此setvisibility代码在解析后才执行)。有很多方法可以解决这个问题,但我建议搜索progressbar和asynctask(还有其他方法也可以实现这一点,但基本上你需要同时启动一个非阻塞的displayprogressbar方法来进行解析,然后在displayprogressbar中检查解析过程的完成情况,然后再返回)。 - logray
我猜你是将带有进度条的RelativeLayout添加到当前布局文件中,这很可能是不正确的,会将其推出可视区域,使其不可见。但是,如果没有看到完整的布局文件,这只是一个猜测。 - user
@Luksprog,已编辑帖子。是的,你说得对。那么,我该怎么办? - azizbekian
你在布局中遗漏了重要的部分,即如何将这三个布局(Linearlayout和其后的两个RelativeLayouts)定位在主RelativeLayout中(它们的宽度/高度、定位规则等)。如果这仍然失败,请参考logray所说的内容。 - user
5个回答

2
无需操作您的 ProgressBar 的可见性。 ListView 具有称为“空视图”的功能,仅在您的 ListView 为空时显示。 这是您需要执行的操作:
  1. 如果您的活动扩展了ListActivity,请使用android:id =“@ android:id / list”作为ListView的ID,并使用android:id =“@ android:id / empty”作为ProgressBar的ID。
  2. 如果您不扩展ListActivity,则可以使用ListViewsetEmptyView()方法执行相同的操作。
以上方法最适合您不需要清空ListView(例如,数据仅在活动启动时加载)。 如果之后需要设置数据,则最好使用ViewSwitcher,它还具有很大的优势-它允许您应用过渡动画。 有关ViewSwitcher的详细信息,请参阅this

1

尝试使用像上面提到的异步任务。

/**
 * this class performs all the work, shows dialog before the work and dismiss it after
 */
 public class ProgressTask extends AsyncTask<String, Void, Boolean> {

public ProgressTask(ListActivity activity) {
    this.activity = activity;
    dialog = new ProgressDialog(context);
}

/** progress dialog to show user that the backup is processing. */
private ProgressDialog dialog;
/** application context. */
private ListActivity activity;

protected void onPreExecute() {
    this.dialog.setMessage("Progress start");
    this.dialog.show();
}

    @Override
protected void onPostExecute(final Boolean success) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }


    //do whatever with your data
}

protected Boolean doInBackground(final String... args) {
   try{    
      //parsing of your website here...

      return true;
   } catch (Exception e)
      Log.e("tag", "error", e);
      return false;
   }
  }
}

使用模态对话框有一个缺点 - 它们会阻塞整个窗口。用户可能希望在加载过程中继续与应用程序交互。 - Andrii Chernenko

1

RelativeLayoutListView重叠导致你的ProgressBar无法显示,请按照以下方式修改布局并查看效果:

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

    <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_below="@id/layout1"/>

    <RelativeLayout 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        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>

另外可以看看我的一个回答,可能会对你有帮助。


1

您不需要将ProgressBar嵌套在另一个RelativeLayout中。只需将其居中放置在主布局中,并将其放置在最后,以便它保持在顶部。

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

   // the rest  

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />   

</RelativeLayout>

0

对于那些对答案感兴趣的人。

只编写了主要标签。

<RelativeLayout
    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_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout1" >

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/layout1" />

    </RelativeLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout1"
        android:gravity="center"
        android:visibility="gone"
        android:background="#DDE7E7E8"
        android:id="@+id/pBar" >

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

    </LinearLayout>

</RelativeLayout>

还有 AsyncTask

private class Parsing extends AsyncTask<Void, Void, Void>
{
    private LinearLayout pBar; 

    private Parsing()
    {
        pBar = (LinearLayout) findViewById(R.id.pBar);
    }

    @Override
    protected Void doInBackground(Void... params) 
    {
        parsingHere();
        return null;
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        pBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(Void result) 
    {
        super.onPostExecute(result);
        pBar.setVisibility(View.INVISIBLE);
    }

}

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