2012-12-04 25 views
5

我有EditText,ButtonListView的观点。 按钮的onClick()解析某个网站(此部分工作正常),但它需要一些时间才能在ListView中显示解析的信息,因此我想在该地点显示小型ProgressBar,其中ListView应在一段时间后发生。如何在做一些工作时在视图上显示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> 

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

但这不起作用。

我该如何解决这个问题?

+0

*但这不起作用。* - 这不是很有帮助。更好地解释什么是问题? – Luksprog

+0

我看不到任何显示的“ProgressBar”。它的工作原理与不添加ProgressBar琐事一样。 – azizbekian

+2

同意Luksprog,但我会愿意猜你的解析是封锁(所以setvisibility代码不会执行,直到解析后)。有很多方法可以解决这个问题,但我会建议搜索progressbar和asynctask(还有其他的方法可以完成这个任务,但基本上你需要在你做同时启动一个非阻塞的displayprogressbar方法解析,然后在返回之前在displayprogressbar中检查完成解析过程)。 – logray

回答

2

有没有必要来操纵你的ProgressBar的知名度。 ListView有一个称为“空视图”的功能,仅在您的ListView为空时显示。下面是你需要做的,使用它是什么:

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

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

0

尝试使用上述的异步任务。

/** 
* 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; 
    } 
    } 
} 
+0

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

1

RelativeLayoutListView隐藏了ProgressBar .Modify这样的布局,看看它是如何去:

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

也可以看看at one of my answers,对于有些类似的问题。

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); 
    } 

} 
相关问题