首页 文章

如何使用asynctask实现旋转动画ProgressDialog

提问于
浏览
1
  • 我在使用此代码执行asynctask之前在progressdialog中显示我的旋转动画

ProgressDialog pDialog = ProgressDialog.show(getActivity(),null,null,true,false); pDialog.setContentView(R.layout.loading);

这是 R.layout.loading xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ln_loadingcontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00000000">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Large"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/tv_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        android:textSize="24dp"
        android:textColor="#ffffff"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"/>
</LinearLayout>
  • 然后我执行我的asynctask . 当asynctask doInbackground我的ProgressDialog不是显示动画时的问题 .

  • 在asynctask onPostExecute后,我的ProgressDialog是开始动画 .

为什么在doInbackground上我的ProgressDialog不显示动画?

怎么解决?

我想在执行asynctask之前在对话框中显示旋转动画,并在asynctask excute完成后隐藏对话框 .

EDIT

1.global variable of ProgressDialog

private ProgressDialog pDiaalog;

2.in oncreate of activity

gv_table.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        progressDialog = ProgressDialog.show(getActivity(), null, null, true, true);
        progressDialog.setContentView(R.layout.loading);

        MyTask t = new MyTask();
        t.execute();
    }
});

3.MyTask

private class MyTaskextends AsyncTask<String, Void, String>{

    public MyTask(){
    }

    @Override
    protected String doInBackground(String... params) {
        RunExecute();
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (pDialog != null) {
            if (pDialog.isShowing())
                pDialog.hide();
        }
    }

    @Override
    protected void onPreExecute() {
        if(IsShowProgress) {
            if (pDialog != null)
                pDialog.show();
        }

        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

当MyTask doInBackground时,progressdialog不会旋转(doInBackground使用时间约为5秒) . 但是在onPostExecute完成之后它又开始旋转了 . 我通过禁止“pDialog.hide();”来测试它 . 在onPostExcute;

1 回答

  • 0
    ProgressDialog pDialog;
    onPreExecure()
    pDialog = ProgressDialog.show(MainActivity.this, null, null, true, false); 
            pDialog.setContentView(R.layout.loading);
    onPostExecute()
    if(pDialog.isShowing())
    {
        pDialog.dismiss();
    }
    

相关问题