在我的新应用中,我需要在进行一些处理时显示动画(例如,将数据发送到服务器,从Web服务读取数据等).
像这样:
当我想实现类似的东西时,这就是我曾经做过的事情:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/sincronizarSpinnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
android:id="@+id/pbHeaderProgress"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
如您所见,我有两个嵌套的相对布局.默认情况下,第一个布局是不可见的(android:visibility =“ gone”),并且只有在启动Service,AsyncTask或异步技术时才使它可见.
尽管我过去曾经使用过这种方法,但是现在我的主布局(应该放在第二个相对布局内)更加复杂,但我不确定使事情变得更加复杂不是一个好主意.为我的活动添加另一个嵌套级别.
>有什么方法可以避免必须为所有其他布局添加其他布局
我需要显示微调动画的活动?也许
我不知道有某种模式或良好做法.
>我真的需要担心为我的图层添加另一个嵌套级别吗
活动,知道我已经有两个或三个级别?
谢谢.
解决方法:
您可以简单地使用ProgressDialog并使用setCanceledOnTouchOutside(false),以便在AsyncTask工作时用户无法触摸外部.
这是我在使用AsyncTask的项目中几乎使用的结构代码.您可以申请您的项目:
public class DownloadTask extends AsyncTask {
private ProgressDialog mProgressDialog;
private Context mContext;
public DownloadTask(Context context) {
this.mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = ProgressDialog.show(mContext, "Downloading", "Downloading Data ...");
mProgressDialog.setCanceledOnTouchOutside(false); // main method that force user cannot click outside
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dlg) {
DownloadTask.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(Void... params) {
// do some background work here
}
@Override
protected void onPostExecute(Void result) {
if (this.isCancelled()) {
result = null;
return;
}
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
}
希望这个帮助:)
标签:performance,android-layout,android
来源: https://codeday.me/bug/20191027/1944330.html