2015-07-02 25 views
0

我需要不断更新。其中被添加到一个ProgressDialog查看自定义布局的定制进度。如何更新ProgressDialog中的progressBar?

自定义布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    android:background="@android:color/transparent" > 

    <com.android.library.circleprogress.ArcProgress 
     android:id="@+id/arc_progress" 
     android:background="@android:color/transparent" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     custom:arc_progress="5" 
     custom:arc_stroke_width="10dp" 
     android:layout_centerInParent="true" 
     custom:arc_bottom_text="Updating..."/> 

</RelativeLayout> 

的Java:

public static ProgressDialog createProgressDialog(Context mContext, ViewGroup viewGroup) { 
    ProgressDialog dialog = new ProgressDialog(mContext); 
    try { 
     dialog.show(); 
    } catch (BadTokenException e) { 

    } 
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.progress_dialog, viewGroup); 
    // this is the progressBar I need to update 
    ArcProgress arcProgress = (ArcProgress) view.findViewById(R.id.arc_progress); 
    dialog.setCancelable(false); 
    dialog.setContentView(view); 
    return dialog; 
} 

我打电话来此的doInBackground方法创建对话框从的AsyncTask,所以我需要更新的百分比数使酒吧移动。

... 
protected void onPreExecute() { 
    super.onPreExecute(); 
    pDialog = Util.createProgressDialog(context, null); 
    pDialog.show(); 
} 
@Override 
protected Integer doInBackground(String... params) { 
    String file = Util.getMapFiles().get(bean).toLowerCase() + ".txt"; 
    // this download updates the pDialog.setProgress(percent) 
    Integer total = ftpDb.download(bean, Const.DIR_FTP_INICIO, file, pDialog); 
    return total; 
} 

任何想法,我怎么能实现这一点? With 默认ProgressDialog我可以更新没有问题的百分比栏。

+0

您必须使用onProgressUpdate方法内的异步任务。并在后台调用publishprogress方法 – Sanket990

+0

试试这个---> dialog.setIndeterminateDrawable(getActivity()。getResources()。getDrawable(R.layout.custom_layout)); –

回答

1

创建的接口中的AsyncTask里面,像这样:

public interface AsyncTaskDelegate{ 
    public void updateProgress(int progress); 
} 

然后在你的AsyncTask构造要将此委托的引用,就像这样:

private AsyncTaskDelegate mDelegate; 

public MyAsyncTask(AsyncTaskDelegate del){ 
    this.mDelegate=del; 
} 

在此之后,你可以覆盖在OnProgressUpdate,像这样:

protected void onProgressUpdate(Integer... progress) { 
    this.mDelegate(progress[0]); 
} 

然后在doInBackground,whene您想要更新您调用publishUpdate(someinteger)方法的进度。

请记住保存对您的实施代理的进度条的引用,并在其中使用progressbar.setProgress()。