【问题标题】:Calling DialogFragment from Fragment (not FragmentActivity)?从 Fragment(不是 FragmentActivity)调用 DialogFragment?
【发布时间】:2014-11-11 07:13:30
【问题描述】:

我确实有一个 FragmentActivity,其中包含一个 Fragment 列表(带有在它们之间导航的方法)。 在其中一个片段中,我需要调用 DialogFragment 以在该片段中包含的图片上显示“缩放”。

但是好像不能直接从Fragment调用DialogFragment。

有什么方法可以对 FragmentActivity 进行某种“回调”以使其在片段上显示 DialogFragment。

或者直接从 Fragment 调用它只是一个“小故障”。

如果是这样,我有什么选择?

【问题讨论】:

  • @blackbelt 我不能,因为他无法解析方法显示

标签: android android-fragments android-fragmentactivity


【解决方案1】:

当您创建一个新的Dialog 时,您可以使用Fragment 中的这个(非常)简单的方法简单地调用它。

DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");

如果您想使用自己的对话框,请使用那种代码。

public class MyDialogFragment extends DialogFragment
{
    //private View pic;

    public MyDialogFragment()
    {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, new LinearLayout(getActivity()), false);

        // Retrieve layout elements
        TextView title = (TextView) view.findViewById(R.id.text_title);

        // Set values
        title.setText("Not perfect yet");

        // Build dialog
        Dialog builder = new Dialog(getActivity());
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
        builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        builder.setContentView(view);
        return builder;
    }
}

【讨论】:

  • 似乎适用于 DialogFragment。但是当我创建: public class MyDialogFragment extends DialogFragment {} With a "oncreateView" inside (calling a xml) 它会中断并向我显示此消息:“无法解析方法显示”但是由于 MyDialogFrament 扩展 DIAlogFragments 没有任何覆盖,不应该这样在每种情况下工作?
  • 我已根据您的对话请求使用示例更新了我的答案。
  • 一旦你正确理解了每一个片段的精妙之处,你就不会再使用Activity ;)
  • 但是如果我使用基本的 DialogFragment 代替它突然可以解决方法“show”。由于 MyDialogFragment 扩展了 DialogFragment 并且仅覆盖 onCreateDialog,看来方法 show 必须在某些部分与 onCreate 链接?否则我不明白为什么它在一种情况下不起作用而在另一种情况下起作用,因为两者之间应该没有区别。
  • insantiate(Context, String) 在 Java 中已弃用
【解决方案2】:

如果您需要在片段中显示片段对话框会有所帮助

对话框片段

public class DialogBoxFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
        getDialog().setTitle("simple dialog");
        return rootView;
    }
}

现在将片段对话框显示为片段

DialogFragment dialogFragment = new DialogFragment ();
                            dialogFragment.show(getActivity().getFragmentManager(),"simple dialog");

【讨论】:

  • 从这个DialogFragment取回数据来调用Fragment怎么样?
【解决方案3】:

检查导入语句。 如果我们使用

ExampleDialogFragment dialog = new ExampleDialogFragment ();
dialog .show(getFragmentManager(), "example");

然后确保导入

import android.app.DialogFragment;
import android.app.Fragment;

不是来自支持库。

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;

【讨论】:

    【解决方案4】:

    对我来说,是这样的:https://stackoverflow.com/a/25056160/2413303

    最重要的部分是您的对话片段需要有一个Callback

    public class MyFragment extends Fragment implements MyDialog.Callback
    

    这有点像这样

    public class MyDialog extends DialogFragment implements View.OnClickListener {
    
    public static interface Callback
    {
        public void accept();
        public void decline();
        public void cancel();
    }
    

    您让 Activity 从 Fragment 为您显示对话框:

        MyDialog dialog = new MyDialog();
        dialog.setTargetFragment(this, 1); //request code
        activity_showDialog.showDialog(dialog);
    

    showDialog() 对我来说是以下方法:

    @Override
    public void showDialog(DialogFragment dialogFragment)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        dialogFragment.show(fragmentManager, "dialog");
    }
    

    然后你回调你的目标片段:

    @Override
    public void onClick(View v)
    {
        Callback callback = null;
        try
        {
            callback = (Callback) getTargetFragment();
        }
        catch (ClassCastException e)
        {
            Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
            throw e;
        }
    
        if (callback != null)
        {
            if (v == acceptButton)
            {   
                callback.accept();
                this.dismiss();
            }
            else if (...) {...}
        }
        else
        {
            Log.e(this.getClass().getSimpleName(), "Callback was null.");
        }
    }
    

    【讨论】:

    • 我在DialogFragment 中使用的是onCreateView() 而不是onCreateDialog()
    • 当在 public void showDialog(DialogFragment dialogFragment) { FragmentManager fragmentManager = getSupportFragmentManager(); dialogFragment.show(fragmentManager, "对话框");他无法解析方法“getSupportFragmentManager();
    • FragmentActivity 中,您可以通过附加到onAttach() 中的Fragment 的接口调用它。请参阅链接的答案,因为这更深入,我只是不想重复 everything 两次而只是复制粘贴,我在这里只留下了重要的元素。
    【解决方案5】:

    试试我在自己的项目中做的这个简单的课程:

            public class UIDialogMessage extends DialogFragment {
    
        public static UIDialogMessage newInstance(int aTitleID, int aMessageID) {
            return newInstance(aTitleID, aMessageID, true);
        }
    
        public static UIDialogMessage newInstance(int aTitleID, int aMessageID, boolean aDoIt) {
            UIDialogMessage frag = new UIDialogMessage();
            Bundle args = new Bundle();
            args.putInt("titleID", aTitleID);
            args.putInt("messageID", aMessageID);
            args.putBoolean("keyBoolDoSomething", aDoIt);
            frag.setArguments(args);
            return frag;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int mTitleID = getArguments().getInt("titleID");
            int mMessageID = getArguments().getInt("messageID");
            final boolean mDoIt= getArguments().getBoolean("keyBoolDoSomething", true);
    
            return new AlertDialog.Builder(getActivity())
                    .setTitle(mTitleID)
                    .setMessage(mMessageID)
                    .setPositiveButton(getResources().getString(R.string.dialog_button_gotcha),
                            new DialogInterface.OnClickListener() {
    
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.dismiss();
                                    if (mDoIt)
                                        doIt();
                                }
                            })
                    .create();
        }
    
        private void doIt() {
            ...
        }
    }
    

    您可以从 Fragment 调用,如下所示:

    showDialog(R.string.dialog_title, R.string.dialog_message, false);
    
    private void showDialog(int aTitleID, int aMessageID, boolean aDoIt) {
            DialogFragment uiDialogMessage = UIDialogMessage.newInstance(aTitleID, aMessageID, aDoIt);
            uiDialogMessage.show(getFragmentManager(), "dialog");
        }
    

    【讨论】:

      【解决方案6】:

      我有同样的问题 通过导入解决

      import android.support.v4.app.ListFragment;
      

      而不是

      import android.app.ListFragment;
      

      【讨论】:

        【解决方案7】:

        你的调用片段

        public class Order_Today extends Fragment {
            public void callDialogFragment() {
                DialogFragmentToOpen add = new DialogFragmentToOpen();
                add.show(getActivity().getSupportFragmentManager(),"dialog");
            }
        }
        

        你的对话片段

        public class DialogFragmentToOpen extends DialogFragment {}
        

        【讨论】:

        • 不错的尝试,但要加上一些解释,以便每个人都能轻松理解
        【解决方案8】:
         ShowValueDialog().showNow(
                                this@OrderFragment.childFragmentManager,
                                "your string"
                            )
        
        **or You Can use this**
        
        this@OrderFragment.parentFragmentManager?.let { it1 -> ShowValueDialog().showNow(it1,"your string") }
        

        【讨论】:

          猜你喜欢
          • 2013-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-16
          相关资源
          最近更新 更多