Android 从 Fragment(不是 FragmentActivity)调用 DialogFragment?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25887373/
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 09:47:37  来源:igfitidea点击:

Calling DialogFragment from Fragment (not FragmentActivity)?

androidandroid-fragmentsandroid-fragmentactivity

提问by Elie Page

Here's my problem : I do have a FragmentActivity wich contains a Fragment list (with methods to navigate between them) In one of thoose Fragments i need to call a DialogFragment to display a "zoom" on a Picture contained in that fragment.

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

But it seems that you can't call a DialogFragment directly from a Fragment.

但似乎不能直接从 Fragment 调用 DialogFragment。

Is there any way to get somekind of "callback" to the FragmentActivity to make this display the DialogFragment over the fragment.

有什么方法可以让 FragmentActivity 获得某种“回调”,使其在片段上显示 DialogFragment。

Or simply a "glitch" to call it directly from the Fragment.

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

If it's the case do you guyz knows a good tutorial about it ?

如果是这种情况,您知道有关它的好教程吗?

Best regards,

此致,

Elie Page

埃利佩奇

采纳答案by Manitoba

When you create a new Dialog, you can simply call it using this (very) simple method from a Fragment.

当你创建一个 new 时Dialog,你可以简单地使用这个(非常)简单的方法从Fragment.

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

If you want to use your own dialog, please use that kind of code.

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

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

回答by Amit

Check for import statements. If we use

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

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

Then make sure to import

然后确保导入

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

not from the support library.

不是来自支持库。

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

回答by PradeepNama

it will help if you need to show a fragment dialog inside a fragment

如果您需要在片段内显示片段对话框,它将有所帮助

Dialogfragment

对话片段

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

now showing the fragment dialog into a fragment

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

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

回答by Khalid

I had same issue solved by import

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

import android.support.v4.app.ListFragment;

instead of

代替

import android.app.ListFragment;

回答by EpicPandaForce

For me, it was the following: https://stackoverflow.com/a/25056160/2413303

对我来说,它是以下内容:https: //stackoverflow.com/a/25056160/2413303

The most important parts are that you need to have a Callbackfor your dialog fragment:

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

public class MyFragment extends Fragment implements MyDialog.Callback

Which kinda works like this

哪一种像这样工作

public class MyDialog extends DialogFragment implements View.OnClickListener {

public static interface Callback
{
    public void accept();
    public void decline();
    public void cancel();
}

You make the Activity show the dialog for you from the Fragment:

您让活动从片段中为您显示对话框:

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

Where showDialog()for me was the following method:

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

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

And you call back onto your target fragment:

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

@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.");
    }
}

回答by Ciro Rizzo

Try this simple class I did in a myown project:

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

        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() {
        ...
    }
}

and you can call from a Fragment as shown below:

您可以从 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");
    }