【问题标题】:How to update a progressBar inside a ProgressDialog?如何更新 ProgressDialog 中的 progressBar?
【发布时间】:2015-07-02 12:20:12
【问题描述】:

我需要更新自定义布局中的 custom progressBar,该布局将作为 View 添加到 ProgressDialog

自定义布局:

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

我在 AsyncTaskdoInBackground 方法中调用此创建对话框,因此我需要更新百分比数字以使条形图移动。

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

有什么想法可以实现吗? 使用 default ProgressDialog 我可以毫无问题地更新百分比条。

【问题讨论】:

  • 异步任务里面的 onProgressUpdate 方法你必须使用。并在后台调用 publishprogress 方法
  • 试试这个---> dialog.setIndeterminateDrawable(getActivity().getResources().getDrawable(R.layout.custom_layout));

标签: android android-progressbar


【解决方案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 中,每当您想更新进度时,您都调用方法 publishUpdate(someinteger)。

记得在你实现委托的地方保存对你的进度条的引用,并在那里使用progressbar.setProgress()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    相关资源
    最近更新 更多