【问题标题】:How to Update a ProgressDialog Fragment如何更新 ProgressDialog 片段
【发布时间】:2012-11-05 17:30:59
【问题描述】:

我需要从AsyncTask 中更新进度对话框片段。我知道如何使用旧的 ProgressDialog 来做到这一点,但现在这种显示对话框的方式已被弃用,我正在尝试对 Fragments 做同样的事情。

这是片段代码:

    public static class CustomProgressDialogFragment extends SherlockDialogFragment {

        static CustomProgressDialogFragment newInstance(Object... params){
            CustomProgressDialogFragment df = new CustomProgressDialogFragment();
            Bundle bundle = new Bundle();

            // Populate bundle with params (not shown)

            df.setArguments(bundle);
            return df;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setCancelable(false);
            int style = DialogFragment.STYLE_NORMAL, theme = 0;
            setStyle(style,theme);
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Bundle bundle = this.getArguments();

            ProgressDialog dialog = new ProgressDialog(getActivity());

            // Get params from bundle and customize dialog accordingly (not shown)

            dialog.setCancelable(false);
            dialog.setIndeterminate(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

            return dialog;
        }
    }

这就是我展示它的方式:

    FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();       
    CustomProgressDialogFragment dialogFragment = CustomProgressDialogFragment.newInstance(<My params here>);
    ft.disallowAddToBackStack();
    ft.add(dialogFragment, stringSet.tag);      
    ft.commitAllowingStateLoss();

现在我需要通过 AsyncTask 的对话框调用 incrementProgressBy。有没有办法从 FragmentManager 检索底层对话框?这样做的正确方法是什么? (如果重新创建对话框,我不想泄露引用)。

提前致谢。

【问题讨论】:

    标签: java android android-fragments progressdialog


    【解决方案1】:

    这可以通过调用DialogFragment.getDialog() 来实现。 DialogFragment 可以像往常一样检索,使用FragmentManager.findFragmentByTag

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多