DialogFragment包含对话框和片段功能

时间:2015-03-16 16:13:30

标签: android dialog fragment android-dialogfragment

我尝试创建自定义对话框,执行两项操作: a)是一个带有两个按钮和一个标题的对话框(很简单) b)可以处理片段事务(不那么简单)

首先,我创建了一个典型的Dialog实现:

public class MyDialog extends DialogFragment {

...

static GeofenceMapDialog newInstance(String str){
    ...
    //Bundle stuff
}


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.fragment_mydialog, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setView(view)
            .setTitle(R.string.geofence_title)
            .setPositiveButton(R.string.geofence_ready_button, (dialog, id) -> {
               //Do stuff
            })
            .setNegativeButton(R.string.geofence_cancel_button, (dialog, id) -> {

            });


    return builder.create();
}

}

好。但是,通过此实现,我不知道如何使用FragmentTransactions。

在onCreateDialog中添加getChildFragmentManager给了我一个没有视图的例外。将所有内容移动到onCreateView上,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_mydialog, null);

    addStuff.setOnClickListener(v -> getChildFragmentManager().beginTransaction().add(R.id.timeSettings, new TimePickerFragment()).commit());

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setView(view)
            .setTitle(R.string.title)
            .setPositiveButton(R.string.button, (dialog, id) -> {
                //do stuff
            })
            .setNegativeButton(R.string.geofence_cancel_button, (dialog, id) -> {

            });

    builder.create();

    return view;
}

解决片段事务的问题。但是我的对话功能现在不起作用。

任何想法如何使用两者?我尝试这样做,但是在onCreateDialog完成后我无法对视图进行充气......

编辑: 这是stacktrace,当我将所有内容移动到onCreateDialog并进行片段事务时。

    03-17 09:18:35.693    3639-3639/com.example.mydialog.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.mydialog.debug, PID: 3639
        java.lang.IllegalStateException: Fragment does not have a view
                at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1746)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:937)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
                at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:157)
                at android.app.ActivityThread.main(ActivityThread.java:5356)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

为了实现这一点,我模仿了警告对话框布局构建器。

public class MyDialogFragment extends DialogFragment {


    public MyDialogFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my_dialog, container, false);


        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getChildFragmentManager().beginTransaction().add(R.id.timeSettings, new OtherFragment()).commit();
            }
        });

        //setup negative and positive buttons just like the other button
        view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something
                dismiss()
            }
        });
        return view;

    }
}

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            new MyDialogFragment().show(getFragmentManager(),"dialog");
        }
    }
}

fragment_my_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:gravity="center">

    <LinearLayout
        android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:minHeight="48dp">

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/message"
                    style="@android:style/TextAppearance.Material.Subhead"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingStart="24dp"
                    android:paddingTop="24dp"
                    android:paddingEnd="24dp"
                    android:text="Message To User Here"/>

                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/timeSettings"/>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/buttonPanel"
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutDirection="locale"
        android:orientation="horizontal"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp">

        <Button
            android:id="@+id/button"
            style="?android:attr/buttonBarNegativeButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Stuff"/>

        <!-- Change this View to Space someday when minimum API > 14 -->
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:visibility="invisible"/>

        <Button
            android:id="@+id/button_cancel"
            style="?android:attr/buttonBarNegativeButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"/>

        <Button
            android:id="@+id/button_ok"
            style="?android:attr/buttonBarPositiveButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK"/>
    </LinearLayout>

</LinearLayout>

按钮按下之前: Before Button Press 按下按钮后,将片段添加到对话框中: After Button Press