加载器在调用 web api Android 时不显示

我有以下加载程序 xml -


custom_progress_dialog.xml(布局)


<?xml version="1.0" encoding="utf-8" ?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/progres"

    android:layout_width="100dp"

    android:layout_height="100dp"

    android:layout_gravity="center"

    android:layout_centerVertical="true"

    android:layout_centerHorizontal="true"

    android:background="#000000" >

  <ProgressBar

      android:indeterminate="true"

      style="?android:attr/progressBarStyleLarge"

      android:layout_width="match_parent"

      android:layout_height="match_parent"

      android:layout_margin="20dp"

      android:layout_centerInParent="true"

      android:indeterminateDrawable="@drawable/custom_progress_bar" />

</RelativeLayout>

custom_progress_bar.xml(可绘制)


<?xml version="1.0" encoding="utf-8" ?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"

    android:pivotX="50%"

    android:pivotY="50%"

    android:fromDegrees="0"

    android:toDegrees="360">

  <shape android:shape="ring" android:innerRadiusRatio="3"

      android:thicknessRatio="8" android:useLevel="false">

    <size

        android:width="76dip"

        android:height="76dip" />

    <gradient

        android:type="sweep"

        android:useLevel="false"

        android:startColor="@android:color/transparent"

        android:endColor="#00FF00"

        android:angle="0" />

  </shape>

</rotate>

然后有一个ShowLoading方法 -


public static Dialog ShowLoading(Context context)

{

    Dialog dialog = new Dialog(context, Android.Resource.Style.ThemeTranslucentNoTitleBar);

    dialog.SetContentView(Resource.Layout.custom_progress_dialog);

    dialog.Window.SetGravity(GravityFlags.Center);

    dialog.SetCancelable(true);

    dialog.CancelEvent += delegate { dialog.Dismiss(); };

    dialog.Show();

    return dialog;

}


我遇到的问题是,当我调用 Web 服务时,它没有显示进度条,并且应用程序基本上冻结,直到对 Web API 的调用完成。


我什至从方法中删除了 web api 调用,以测试进度条在调用时是否正确显示。


如何在 api 调用进行时显示进度条?


慕雪6442864
浏览 46回答 1
1回答

梵蒂冈之花

不要在主线程调用api,使用AsyncTask调用api像public class MyAsync extends AsyncTask<Void, Void, Void> {&nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPreExecute() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onPreExecute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//show progress dialog here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Void doInBackground(Void... voids) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //call api here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPostExecute(Void aVoid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onPostExecute(aVoid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //hide progress dialog here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }执行它使用这个new MyAsync().execute();
打开App,查看更多内容
随时随地看视频慕课网APP