Using FragmentTransaction with a DialogFragment

前端 未结 4 1910
后悔当初
后悔当初 2021-02-05 18:03

So I\'ve created a DialogFragment that is shown as a dialog via this technique

Now that it\'s launched and upon a user interaction within this popup I want to slide in a

4条回答
  •  梦谈多话
    2021-02-05 18:49

    alexanderblom is right that DialogFragment acts as a Dialog, however it can be made to act as a Fragment with setShowsDialog(false);

    In the end the following worked for me:

    File: res/layout/wifidirect_dialog_wifidirect:

    
    
        
    
                
                
                
        
    
        
        
        

    File src/.../WifiDirectDialog.java:

    public class WiFiDirectDialog extends DialogFragment  {
            public static final String TAG = "WifiDirectDialog";
            public static final String DEVICE_LIST_FRAGMENT_TAG = "WIFIDIRECT_DEVICE_LIST_FRAGMENT";
    
    
            public static WiFiDirectDialog newInstance(){                 
                 WiFiDirectDialog wDialog = new WiFiDirectDialog();
                 //We want this Dialog to be a Fragment in fact,
                 //otherwise there are problems with showing another fragment, the DeviceListFragment
                 wDialog.setShowsDialog(false);
                 //wDialog.setStyle(SherlockDialogFragment.STYLE_NORMAL,android.R.style.Theme_Holo_Light_Dialog);
                 //We don't want to recreate the instance every time user rotates the phone
                 wDialog.setRetainInstance(true);
                 //Don't close the dialog when touched outside
                 wDialog.setCancelable(false);
                 return wDialog;
            }
    
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                Log.v(TAG,"onCreateView");
    
                View view = inflater.inflate(R.layout.wifidirect_dialog_wifidirect,container, false);
    
                //Log.v(TAG,"FragmentTransaction started");            
                ListFragment listFragment = new YourListFragment();
    
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.addToBackStack(DEVICE_LIST_FRAGMENT_TAG)
                    .replace(R.id.frag_list,deviceListFragment,DEVICE_LIST_FRAGMENT_TAG)
                    .commit();
                //Log.v(TAG,"FragmentTransaction finished");
    
                return view;
            };
    
            @Override
            public void onActivityCreated(Bundle savedInstanceState){
                Log.v(TAG,"onActivityCreated");
                super.onActivityCreated(savedInstanceState);
    
                Dialog dialog = getDialog();
                dialog.setTitle(R.string.wifidirect_dialog_title);
    
                // Set button listeners etc...///
                Button cancelButton = (Button) view.findViewById(R.id.dialog_wifidirect_cancel);
                cancelButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {                
                        dismiss();
                    }
                });
            }
    

提交回复
热议问题