Android 在 DialogFragment 中显示进度对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23272122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:58:01  来源:igfitidea点击:

Showing progress dialog within DialogFragment

androidandroid-fragmentsfragmentprogressdialog

提问by deimos1988

I would like to show a progress dialog within a dialog fragment.

我想在对话框片段中显示进度对话框。

However when I am using this code

但是,当我使用此代码时

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();

the progress dialog is shown in the fragment that called the dialogFragment, not in the DialogFragment itself.

进度对话框显示在调用 dialogFragment 的片段中,而不是显示在 DialogFragment 本身中。

What did I do wrong?

我做错了什么?

回答by Simon

Here you go.

干得好。

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;
    }
}

回答by savepopulation

Here's a simple loading dialog fragment which plays a gif as a animation drawable

这是一个简单的加载对话框片段,它播放 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();
        }
    }
}

And you can show and hide it with:

你可以显示和隐藏它:

 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();
      }
   }

Edit: By the way it's not a good practice to show a dialog fragment for async operations such as networking.

编辑:顺便说一下,为异步操作(如网络)显示对话框片段不是一个好习惯。

回答by FinalDark

Show the progressDialog in onStartmethod inside DialogFragmentclass

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();
}

回答by mike20132013

Try this :

尝试这个 :

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();

Or

或者

If dynamically using ProgressDialog;

如果动态使用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();

On Data loaded,

在加载数据时,

prog.dismiss();

Now, if the progress bar is not showing inside your dialog, you will have to set a custom dialog and define your progress bar inside that custom dialog.

现在,如果进度条没有显示在您的对话框中,您将必须设置一个自定义对话框并在该自定义对话框中定义您的进度条。

回答by SacreDeveloper

Use ProgressBar inside your dialog fragment. add progressBar view in your xml code .

在对话框片段中使用 ProgressBar。在您的 xml 代码中添加 progressBar 视图。

In Your code:

在您的代码中:

ProgressBar progressBar = view.findViewById(R.id.progress);
progressBar .show();

when finish:

完成时:

 progressBar.hide();

回答by Himanshu Narang

I know it is a very old post, but I want to tell a solution. If I am not wrong you are trying to show your progress dialogbefore onStart lifecycle method. Show progress dialogin onResume method.

我知道这是一个很老的帖子,但我想告诉一个解决方案。如果我没有错的话,你是在试图展示你progress dialog之前的onStart lifecycle method. 显示progress dialogonResume method.

回答by Mahbubur Rahman Khan

Just use this class as progress dialog....

只需将此类用作进度对话框....

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();
    }
}

Uses....

用....

public class DialogFragment extends BaseFragment {...}