我有以下加载程序 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 调用进行时显示进度条?
1 回答
梵蒂冈之花
TA贡献1900条经验 获得超5个赞
不要在主线程调用api,使用AsyncTask调用api
像
public class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//show progress dialog here
}
@Override
protected Void doInBackground(Void... voids) {
//call api here
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//hide progress dialog here
}
}
执行它使用这个
new MyAsync().execute();
- 1 回答
- 0 关注
- 45 浏览
添加回答
举报
0/150
提交
取消