简体   繁体   English

Android应用程序自定义对话框架构

[英]Android app custom dialogs architecture

I have a MainActivity with some Fragments changed by Navigation Drawer. 我有一个MainActivity,导航抽屉改变了一些碎片。 I would like to prepare some custom Dialogs and want to open them by my custom class (neither activity nor fragment class). 我想准备一些自定义对话框,并希望通过我的自定义类(既不是活动也不是片段类)打开它们。 Is it possible to do something like this without implementing method for each custom Dialog in MainActivity or Fragment? 如果不为MainActivity或Fragment中的每个自定义对话框实现方法,是否可以执行此类操作?

I'm not sure I understand 100% what the problem is, but if it's with having many dialogs which you don't want to set up using many different methods in your activity, have the dialogs inherit from a new class MyDialogFragment with a static newInstance method, then pass the specific dialog class you want to instantiate as a parameter. 我不确定我100%理解问题是什么,但是如果你想要在活动中使用许多不同的方法设置许多对话框,那么让对话框继承一个带有静态的新类MyDialogFragment newInstance方法,然后传递要作为参数实例化的特定对话框类。

So 所以

public class MyDialogFragment extends DialogFragment {
    ...
    public static DialogFragment newInstance() {
        // Overwrite this method in your dialogs to do what you have to do
    }
}

Then in your activity: 然后在你的活动中:

private void instantiateDialog(Class<? extends MyDialogFragment dialogClass) {
    MyDialogFragment dialog = c.newInstance();
    dialog.show(getFragmentManager(), c.getCanonicalName());
}

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

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