我想在对话框片段中显示进度对话框。
但是当我使用此代码时
ProgressDialog prog = new ProgressDialog(ctx);
prog.setTitle(getString(R.string.pleaseWait));
prog.setMessage(getString(R.string.webpage_being_loaded));
prog.setCancelable(false);
prog.setIndeterminate(true);
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.show();
进度对话框显示在调用dialogFragment的片段中,而不是在DialogFragment本身中。
我做错了什么?
答案 0 :(得分:21)
你走了。
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class ProgressDialogFragment extends DialogFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setCancelable(false);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
dialog.setTitle(getString(R.string.pleaseWait));
dialog.setMessage(getString(R.string.webpage_being_loaded));
dialog.setIndeterminate(true);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
return dialog;
}
}
答案 1 :(得分:5)
这是一个简单的加载对话框片段,它将gif作为动画可绘制的
public class LoadingDialogFragment extends DialogFragment {
public static final String FRAGMENT_TAG = "LoadingFragment";
private ImageView ivLoading;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_loading, container, false);
if (rootView != null) {
ivLoading = (ImageView) rootView.findViewById(R.id.iv_loading);
ivLoading.setBackgroundResource(R.drawable.anim_drawable);
}
return rootView;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
AnimationDrawable loadingAnimation = (AnimationDrawable) ivLoading.getBackground();
loadingAnimation.start();
}
}
}
你可以用以下方式显示和隐藏它:
public void showLoadingDialog() {
LoadingDialogFragment fragment = (LoadingDialogFragment) getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new LoadingDialogFragment();
fragment.setCancelable(false);
getSupportFragmentManager().beginTransaction()
.add(fragment, LoadingDialogFragment.FRAGMENT_TAG)
.commitAllowingStateLoss();
// fragment.show(getSupportFragmentManager().beginTransaction(), LoadingDialogFragment.FRAGMENT_TAG);
}
}
public void hideLoadingDialog() {
LoadingDialogFragment fragment = (LoadingDialogFragment) getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.FRAGMENT_TAG);
if (fragment != null) {
// fragment.dismissAllowingStateLoss();
getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
}
}
编辑:顺便说一下,为网络等异步操作显示对话框片段并不是一个好习惯。
答案 2 :(得分:2)
试试这个:
ProgressDialog prog = (ProgressDialog)your_view.findViewById(R.id.yourprogress_id);
prog.setTitle(getString(R.string.pleaseWait));
prog.setMessage(getString(R.string.webpage_being_loaded));
prog.setCancelable(false);
prog.setIndeterminate(true);
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.show();
或
如果动态使用ProgressDialog;
ProgressDialog prog= new ProgressDialog(getActivity());//Assuming that you are using fragments.
prog.setTitle(getString(R.string.pleaseWait));
prog.setMessage(getString(R.string.webpage_being_loaded));
prog.setCancelable(false);
prog.setIndeterminate(true);
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.show();
在加载数据时,
prog.dismiss();
现在,如果您的对话框中没有显示进度条,则必须设置自定义对话框并在该自定义对话框中定义进度条。
答案 3 :(得分:1)
在对话框片段中使用ProgressBar。 在xml代码中添加progressBar视图。
在您的代码中:
ProgressBar progressBar = view.findViewById(R.id.progress);
progressBar .show();
结束时:
progressBar.hide();
答案 4 :(得分:1)
我知道这是一个非常古老的帖子,但我想告诉解决方案。如果我没有错,您试图在progress dialog
之前显示onStart lifecycle method
。在progress dialog
中显示onResume method
。
答案 5 :(得分:1)
在 DialogFragment 类
中的 onStart 方法中显示progressDialog@Override
public void onStart() {
super.onStart();
ProgressDialog prog = new ProgressDialog(ctx);
prog.setTitle(getString(R.string.pleaseWait));
prog.setMessage(getString(R.string.webpage_being_loaded));
prog.setCancelable(false);
prog.setIndeterminate(true);
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.show();
}
答案 6 :(得分:0)
只需使用此类作为进度对话框即可。...
package com.microfinance.app.microfinance;
import android.app.ProgressDialog;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;
/**
* Created by USER on 9/18/2018.
*/
public class BaseFragment extends Fragment {
@VisibleForTesting
public ProgressDialog mProgressDialog;
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this.getContext());
mProgressDialog.setMessage("Loading ...");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
@Override
public void onStop() {
super.onStop();
hideProgressDialog();
}
}
使用......
public class DialogFragment extends BaseFragment {...}