DialogFragment使用及总结

DialogFragment使用总结

介绍

DialogFragment在android 3.0时被引入。是一种特殊的Fragment。在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。

优势在哪里

  1. 使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期
  2. DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用
  3. 使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。onCreateView即使用定义的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。

使用方法

  1. 重写onCreateView 将要展示的dialog放在xml文件中,就像创建Fragment一样就可以

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.fragment_dialog1, container, false);
     }
    
  2. 重写onCreateDialog ,在方法中创建dialog并返回

     public Dialog onCreateDialog(Bundle savedInstanceState) {
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
         View inflate = layoutInflater.inflate(R.layout.fragment_dialog1, null, false);
         AlertDialog alertDialog = builder.setView(inflate).setCancelable(true).setMessage("什么鬼").setPositiveButton("好吧", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 onPressDialogBtn.onPress("什么情况");
             }
         }).setNegativeButton("不好", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
             }
         }).create();
         return alertDialog;
     }
    
  3. show出来(1、2 选一即可)

    MyDialogFragment myDialogFragment = new MyDialogFragment();
    myDialogFragment.show(getSupportFragmentManager(), "myDialogFragment");

数据传递:

  1. Activity 给 DialogFragment传值:

    1. 通过setArguments,然后DialogFragment 在onCreateView、onCreateDialog中接收Bundle

      //Activity 中,创建DialogFragment的时候
      MyDialogFragment myDialogFragment = new MyDialogFragment();
      Bundle bundle = new Bundle();
      bundle.putSerializable("data",1);
      myDialogFragment.setArguments(bundle);

      //Fragment中
      Bundle arguments = getArguments();
      int data = (int) arguments.getSerializable("data");

    2. 通过在DialogFragment中 set,设置value

  2. DialogFragment 给 Activity回传:通过接口回调的方式,即在Fragment中定义接口,Activity实现

     //Fragment中
     public OnPressDialogBtn onPressDialogBtn;
    
     public interface OnPressDialogBtn {
         void onPress(String msg);
     }
    
     public void setOnPressDialogBtn(OnPressDialogBtn onPressDialogBtn) {
         this.onPressDialogBtn = onPressDialogBtn;
     }
     //设置监听
     myDialogFragment.setOnPressDialogBtn(this);
    

DialogFragment做屏幕适配

例如需求如下:一个对话框在大屏幕上以对话框的形式展示,而小屏幕上则直接嵌入当前的Actvity中。

  1. 创建一个values-large

  2. 在values 和 values-large中分别创建一个bools.xml

  3. 并添加属性 false ,在values下赋值false,在values-large下赋值true

  4. 然后回去这个值,取值后伪代码如下:

     if(isLarge){
         myFragmentDialog.show(getSupportManager(),"");
     }else{
        getSupportManager().begintrasition().add(myFragmentDialog,R.id.container,"").commit()
     }
    

屏幕旋转

传统的new AlertDialog在屏幕旋转时,第一不会保存用户输入的值,第二还会报异常,因为Activity销毁前不允许对话框未关闭。
而通过DialogFragment实现的对话框则可以完全不必考虑旋转的问题。

建议

可以在它基础上进行封装,封装成多用Dialog的。
Demo下载地址
感谢巨人的肩膀

你可能感兴趣的:(DialogFragment使用及总结)