我有一个关于dialogfragment的问题 - 我通过教程创建了一个带有dialogfragment的类,但在教程中,按钮单击显示了对话框片段 - 我想通过单击MainActivity中的片段选项卡来显示dialogfragment - 我可以&#39 ;弄清楚如何...我有片段GetItem方法,我的代码中有空案例4 - 它是片段,我想通过点击它来打开dialogfragment。有我的代码。感谢您的帮助和解答!对不起,如果有事情不清楚..
-Problem是固定的(即注释) - 我创建了新的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");
}
答案 0 :(得分:1)
您可能想要检测单击哪个标签并像其他任何
一样显示对话框您需要添加TabLayout.OnTabSelectedListener
设置
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
}
}
});
答案 1 :(得分:0)
查看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();
}
}