Android-----DialogFragment的使用

来源:互联网 发布:淘宝详情页面模板 编辑:程序博客网 时间:2024/05/01 23:43

一、DialogFragment是Android针对一些常用场景,对Fragment进行的一次封装。

建立一个类继承DialogFragment,创建一个方法,该方法返回Fragment自身,并且保存住外界传入的类型,public static MyDialogFragment getInstance(int type){ MyDialogFragment f1=new MyDialogFragment (); Bundle bundle=new Bundle()' bundler.putInt("Dialog_Type",type); f1.setArgument(bundler); return f1;}//在onCreateDialog中写入自己的业务逻辑,当前要实现的是根据外部传入的类型,创建不同的FragmentDialog onCreateDialog(Bundle saveInstance){ Dialog dialog=null; int dialogType= getArguments().getInt("Dialog_Type")//拿到通过getInstance方法保存在自身中的值 switch(dialogType){  case XX:    return new AlertDialog()......  break;} return dialog;}

在Activity中调用

MyFragment myFragment=MyFragment.getInstance();if(myFragment!=null){ myFragment.show(getFragmentManager(),tag);}
0 0
原创粉丝点击