简体   繁体   English

片段如何实现dialogfragment

[英]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. 我有一个关于dialogfragment的问题-我通过教程创建了一个使用dialogfragment的类,但是在本教程中,单击按钮显示了dialogfragment-我想通过单击MainActivity中的fragment选项卡显示dialogfragment ..我有Fragment GetItem方法,其中我的代码中有空的case 4-这是片段,我想通过单击它来打开dialogfragment。 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 -问题已修复(即注释)-我创建了新的DialogFragment-xml文件和Java类-来自guides.codepath.com/android/Using-DialogFragment#build-dialo‌g的代码,然后将此代码添加至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 您需要添加一个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 : 参见android 示例

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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