简体   繁体   中英

how to implement dialogfragment in fragment?

I have a question about dialogfragment - I created a class with dialogfragment by tutorial, but in the tutorial, the dialogfragment was shown by button click - I want to show dialogfragment by clicking on fragment tab in my MainActivity - I can't figure out how.. I have Fragment GetItem method, where i have empty case 4 in my code - It's the fragment, where I want to open dialogfragment by clicking on it. there are my codes. Thanks for all help and answers! Sorry if something isn't clear..

-Problem is fixed (viz comments) - I created new DialogFragment - xml file and java class - codes from guides.codepath.com/android/Using-DialogFragment#build-dialo‌​g and then I added this codes to MainActivity

 tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
                int position = tab.getPosition();
                if (position == 4) {
                    showAlertDialog();
                }
            }
        });
    }
    private void showAlertDialog() {
        FragmentManager fm = getSupportFragmentManager();
        EditNameDialogFragment alertDialog = EditNameDialogFragment.newInstance("Some title");
        alertDialog.show(fm, "fragment_alert");
    }

You probably want to detect which tab is clicked and show the dialog like any other

You need to add a TabLayout.OnTabSelectedListener

Set that up

tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
  @Override
  public void onTabSelected(TabLayout.Tab tab) {
      super.onTabSelected(tab);
      int position = tab.getPosition();
      if (position == 4) {
          // Create MediaFragment here
      } 
   }
});

see the android example :

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM