DialogFragment使用及总结
来源:互联网 发布:linux faikmm 编辑:程序博客网 时间:2024/04/29 14:42
DialogFragment使用总结
介绍
DialogFragment在android 3.0时被引入。是一种特殊的Fragment。在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。
优势在哪里
- 使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期
- DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用
- 使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。onCreateView即使用定义的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。
使用方法
重写onCreateView 将要展示的dialog放在xml文件中,就像创建Fragment一样就可以
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_dialog1, container, false);}
重写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;}
show出来(1、2 选一即可)
MyDialogFragment myDialogFragment = new MyDialogFragment();myDialogFragment.show(getSupportFragmentManager(), "myDialogFragment");
数据传递:
Activity 给 DialogFragment传值:
通过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”);- 通过在DialogFragment中 set,设置value
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中。
- 创建一个values-large
- 在values 和 values-large中分别创建一个bools.xml
- 并添加属性 false ,在values下赋值false,在values-large下赋值true
然后回去这个值,取值后伪代码如下:
if(isLarge){ myFragmentDialog.show(getSupportManager(),"");}else{ getSupportManager().begintrasition().add(myFragmentDialog,R.id.container,"").commit()}
屏幕旋转
传统的new AlertDialog在屏幕旋转时,第一不会保存用户输入的值,第二还会报异常,因为Activity销毁前不允许对话框未关闭。
而通过DialogFragment实现的对话框则可以完全不必考虑旋转的问题。
建议
可以在它基础上进行封装,封装成多用Dialog的。
Demo下载地址
感谢洪洋大神的肩膀
- DialogFragment使用及总结
- DialogFragment使用总结
- DialogFragment使用总结
- Android 必知必会 - DialogFragment 使用总结
- 使用DialogFragment
- dialogfragment使用
- DialogFragment 使用
- DialogFragment 使用
- 使用DialogFragment
- DialogFragment使用
- 使用DialogFragment
- DialogFragment使用
- DialogFragment总结与封装
- Using DialogFragment ——使用DialogFragment
- Android:DialogFragment的使用
- Dialog与DialogFragment使用
- ApiDemos--DialogFragment的使用
- android DialogFragment简单使用
- socket之基础讲解
- Centos7.3安装和配置jre1.8
- 在onCreate()中获取某个View的宽高(post()方法)
- oracle几种字符类型总结
- 测试博客第一篇
- DialogFragment使用及总结
- Centos7.3安装和配置Mysql5.7
- Xshell中如何进行多会话设置
- JS简介,JS如何实现输入输出语句、注释
- C# DataTable行转列实现方法
- 毕业了,我是如何胜任Linux运维工程师的
- 位运算详解及竞赛常见用法入门
- hdu 6202 cube cube cube 沈阳区域赛网络赛 (膜拟)
- mybatis动态排序