2012-08-25 54 views
0

我有一个AsyncTask链接到刷新按钮(当我点击我的刷新按钮时,我的AsyncTask被称为)。ProgressBar多重显示

我有我的布局我的进度一的LinearLayout场:

<?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" 
    android:background="@color/grey"> 
    <Button android:id="@+id/refresh_p" 
       android:text="@string/refresh_promo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/custom_button1"/> 

    <LinearLayout android:id="@+id/linearlayoutProgressBar" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center"/> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:listSelector="@color/tabTransparent" 
     android:cacheColorHint="#00000000"/> 
     <!-- <ListView android:id="@+id/list_promo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/>--> 
</LinearLayout> 

在我的AsyncTask:

@Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 

     pb = new ProgressBar(context); 
     LinearLayout ll = (LinearLayout) ((Activity) context).findViewById(R.id.linearlayoutProgressBar); 
     ll.addView(pb); 
     pb.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> promoList) 
    { 
     ... 

     if (pb!=null) 
     { 
      pb.setVisibility(View.GONE); 
      ((LinearLayout)pb.getParent()).removeView(pb); 
     } 
    } 

我的问题是,当我把我的刷新按钮超过2次点击然后多个ProgressBar显示在屏幕上..我只是希望新的ProgressBar在相同的位置替换旧的..

回答

0

我的猜测是,你可能以类似于以下的方式启动您的AsyncTask:

new RefreshTask().execute(params); 

取而代之的是,在您的活动中创建一个实例变量。比方说,这个被命名为mTask并替换上面的代码以下电话:

if(mTask != null && mTask.getState != AsyncTask.State.RUNNING){ 
    mTask = new RefreshTask(); 
    mTask.execute(params); 
} 

这样,您将确保只有一个你的任务的实例在给定时间运行,用户将不得不等待,直到刷新完成后才能开始新的刷新。

如果你希望用户能够取消现有刷新任务并运行一个新的,你将不得不开始一个新的人之前先取消原有之一:

if(mTask != null){ 
    if(mTask.getStatus() == AsyncTask.Status.RUNNING){ 
     mTask.cancel(true); 
    } 
    mTask = new RefreshTask(); 
    mTask.execute(params); 
} 

这样的老任务它的ProgressBar将会被新的替换。

+0

感谢您的支持,我也解决了这个问题,并且它的工作正常。谢谢 – eento