How to display an existing ListFragment in a DialogFragment

前端 未结 4 1072
一个人的身影
一个人的身影 2020-12-16 01:44

I have the following problem:

I have an exisiting ListFragment, but I would like to display this as a dialog.

My first approach was to create a

4条回答
  •  一个人的身影
    2020-12-16 02:17

    When adding a fragment inside another fragment, the documentation says you should do it dynamically (i.e., rather than hardcoding a tag into your layout XML.

    So here is how to do it dynamically. In this case, I add MyListFragment to MyDialogFragment:

    MyDialogFragment.java

    import android.app.Dialog;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.DialogFragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MyDialogFragment extends DialogFragment {
    
        public static final String TAG = MyDialogFragment.class.getSimpleName();
        private static final String ARG_TITLE = "ARG_TITLE";
    
        private EditText mEditText;
    
        public MyDialogFragment() {
            // Empty constructor required for DialogFragment
        }
    
        public static MyDialogFragment newInstance(String title) {
            MyDialogFragment myDialogFragment = new MyDialogFragment();
            Bundle args = new Bundle();
            args.putString(ARG_TITLE, title);
            myDialogFragment.setArguments(args);
            return myDialogFragment;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Dialog dialog = super.onCreateDialog(savedInstanceState);
            Bundle args = getArguments();
            if (args != null) {
                dialog.setTitle(args.getString(ARG_TITLE));
            }
            return dialog;
        }
    
        public void setTitle(String title) {
            Dialog dialog = getDialog();
            dialog.setTitle(title);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.dialog_fragment_selected_products, container, false);
            //addInnerFragment();
    
            Button okButton = (Button)view.findViewById(R.id.okButton);
            okButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dismiss();
                            //dismissAllowingStateLoss();
                        }
                    }
            );
    
            return view;
        }
    
        @Override
        public void onStart() {
            super.onStart();
            //addInnerFragment();
        }
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            addInnerFragment();
        }
    
        public void addInnerFragment() {
    
            FragmentManager childFragmentManager = getChildFragmentManager();
            FragmentTransaction transaction = childFragmentManager.beginTransaction();
            //transaction.add(R.id.fragmentContainer, new MyListFragment());
            transaction.add(R.id.fragmentContainer, MyListFragment.newInstance(MyListFragment.MODE_SELL));
            //transaction.commit();
            transaction.commitAllowingStateLoss();
            childFragmentManager.executePendingTransactions();
    
        }
    
    }
    

    (As you will see, it also contains some functionality to set the title of the dialog.)

    dialog_fragment_selected_products.xml

    
    
    
        
    
        

    Another advantage of doing it this way is that you can create an instance of the inner fragment in order to pass any arguments to it.

    For completeness, here is the code that I use in my activity to show the DialogFragment:

    MyActivity.java

    private void showCurrentItemsDialog() {
    
        MyDialogFragment myDialogFragment = MyDialogFragment.newInstance("cpuk.org");
        //myDialogFragment.setRetainInstance(true);
    
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(myDialogFragment, MyDialogFragment.TAG);
        transaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    
    }
    

提交回复
热议问题