选择DialogFragment的容器

时间:2014-11-03 10:23:14

标签: android android-fragments android-dialog android-dialogfragment

是否可以创建DialogFragment并更改默认容器? 我尝试了DialogFragment.show(Transaction, ...)并在那里设置了容器,然后它告诉我Fragment already added。 DialogFragment的行为与普通的DialogFragment类似,这一点非常重要。

编辑:我认为存在一些误解。我说它应该看起来像正常的DialogFragment"。我的意思是它看起来应该像普通的AlertDialog。

3 个答案:

答案 0 :(得分:1)

我认为,由于DialogFragment来自Fragment,因此您可以在需要时将其用作普通Fragment。它仍应实现所有Fragment生命周期方法。但是,我不确定它是否因特殊目的而覆盖了它们中的任何一个。

答案 1 :(得分:1)

你可以像使用普通片段一样使用dialogFragment。从样本中查看此代码。 在此代码中,它创建一个dialogFragment并将其添加到framlayout,并在按下按钮时将其显示为对话框。

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="top|center_horizontal"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Demonstrates the same fragment
            being shown as a dialog and embedded inside of an activity." />

    <Button android:id="@+id/show_dialog"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="show">
        <requestFocus />
    </Button>

    <View android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    <TextView
            android:id="@+id/inline_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_gravity="center_vertical|center_horizontal"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Fragment embedded inside
        of the activity:" />

    <FrameLayout
            android:id="@+id/embedded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="6dp"
            android:background="#ff303030"
            android:gravity="top|center_horizontal" />

</LinearLayout>

和java代码:

public class FragmentDialogOrActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_dialog_or_activity);

        if (savedInstanceState == null) {
            // First-time init; create fragment to embed in activity.

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            DialogFragment newFragment = MyDialogFragment.newInstance();
            ft.add(R.id.embedded, newFragment);
            ft.commit();

        }

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.show_dialog);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog();
            }
        });
    }


    void showDialog() {
        // Create the fragment and show it as a dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance();
        newFragment.show(getFragmentManager(), "dialog");
    }



    public static class MyDialogFragment extends DialogFragment {
        static MyDialogFragment newInstance() {
            return new MyDialogFragment();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.hello_world, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("This is an instance of MyDialogFragment");
            return v;
        }
    }

}

您的错误可以使用以下代码:

public static void showMyDialogFragment(FragmentManager fm){

    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("MyDialogFragment");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    DialogFragment newFragment = MyDialogFragment.newInstance();
    newFragment.show(ft, "MyDialogFragment");
}

正如您所注意到的,我更改了标记名称,以便您可以区分对话框片段和添加到布局中的普通片段。

答案 2 :(得分:0)

这是DialogFragment

的示例
public static class MyDialogFragment extends DialogFragment {
        int mNum;

    /**
     * Create a new instance of MyDialogFragment, providing "num"
     * as an argument.
     */
    static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");

        // Pick a style based on the num.
        int style = DialogFragment.STYLE_NORMAL, theme = 0;
        switch ((mNum-1)%6) {
            case 1: style = DialogFragment.STYLE_NO_TITLE; break;
            case 2: style = DialogFragment.STYLE_NO_FRAME; break;
            case 3: style = DialogFragment.STYLE_NO_INPUT; break;
            case 4: style = DialogFragment.STYLE_NORMAL; break;
            case 5: style = DialogFragment.STYLE_NORMAL; break;
            case 6: style = DialogFragment.STYLE_NO_TITLE; break;
            case 7: style = DialogFragment.STYLE_NO_FRAME; break;
            case 8: style = DialogFragment.STYLE_NORMAL; break;
        }
        switch ((mNum-1)%6) {
            case 4: theme = android.R.style.Theme_Holo; break;
            case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
            case 6: theme = android.R.style.Theme_Holo_Light; break;
            case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
            case 8: theme = android.R.style.Theme_Holo_Light; break;
        }
        setStyle(style, theme);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // put here any view you want 
        View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Dialog #" + mNum + ": using style "
                + getNameForNum(mNum));

        // Watch for button clicks.
        Button button = (Button)v.findViewById(R.id.show);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // When button is clicked, call up to owning activity.
                ((FragmentDialog)getActivity()).showDialog();
            }
        });

        return v;
    }
}



void showDialog() {
    mStackLevel++;

    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = getFragmentManager().beginTransaction();
      // may you got error here 
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
     // remove fragment from stack will fix the problem 
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
    newFragment.show(ft, "dialog");
}

如果你想在警报按钮中使用它,那么代码是

public static class MyAlertDialogFragment extends DialogFragment {

    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();
    }
}