gpt4 book ai didi

android - 在 fragment 过渡时添加进度对话框

转载 作者:行者123 更新时间:2023-11-29 00:16:59 24 4
gpt4 key购买 nike

我尝试在将一个 Fragment 切换到另一个 Fragment 时显示一个 ProgressDialog,例如消息“请稍候...”或“加载中”页面...”等我需要添加此消息,因为 Fragment 的加载时间即使我添加了 getSupportFragmentManager().executePendingTransactions(); 在提交我的 Fragment 之后 转换,我尝试如下:

    ProgressDialog pd=    ProgressDialog.show(getActivity(), "Please Wait","loading page...");   
getFragmentManager().beginTransaction().replace(R.id.frame_container, new TertanggungPolis()).
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
pd.dismiss();

但是这段代码似乎不起作用,因为我的应用程序中没有显示 ProgressDialog,所以我的代码或方法有问题吗?谢谢

最佳答案

使用这个:

import android.app.ProgressDialog;
import android.content.Context;

public class AndyUtills {

private static ProgressDialog mProgressDialog;


public static void showSimpleProgressDialog(Context context, String title,
String msg, boolean isCancelable) {
try {
if (mProgressDialog == null) {
mProgressDialog = ProgressDialog.show(context, title, msg);
mProgressDialog.setCancelable(isCancelable);
}

if (!mProgressDialog.isShowing()) {
mProgressDialog.show();
}

} catch (IllegalArgumentException ie) {
ie.printStackTrace();
} catch (RuntimeException re) {
re.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void removeSimpleProgressDialog() {
try {
if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
} catch (IllegalArgumentException ie) {
ie.printStackTrace();

} catch (RuntimeException re) {
re.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

}
}

现在使用 className 本身调用这两个方法,您的工作就完成了。当你想显示对话框时调用 show 方法,当你想删除对话框时调用 remove 方法。

现在在您的案例中,您可以在第二个 fragment 的 OnCreateView 方法中调用 remove 方法。

关于android - 在 fragment 过渡时添加进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com