iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android Dialog之DialogFragment详解与使用
  • 216
分享到

Android Dialog之DialogFragment详解与使用

androidandroidstudiodialogkotlin 2023-09-04 10:09:36 216人浏览 八月长安
摘要

一、介绍         在Android开发过程中,经常会有弹窗业务,在正常的弹窗业务中,常用到的是Dialog,Dialog的原理也是通过将view,添加到Dialog中。Dialog自身是一个独立的窗口,和Activity一样,有自己

一、介绍

        在Android开发过程中,经常会有弹窗业务,在正常的弹窗业务中,常用到的是Dialog,Dialog的原理也是通过将view,添加到Dialog中。Dialog自身是一个独立的窗口,和Activity一样,有自己的window管理。如果简单的使用Dialog不方便管理,在后面Google提供了DialogFragment弹窗。

二、DialogFragment的介绍

        看到DialogFragment,从单词可以看到是Dialog与Fragment组合而成,但是在DialogFragment中,DialogFragment其实是一个fragment,内部持有一个Dialog,这样方便了开发者的管理。fragment在碎片管理中其实就是一个View,最终view的显示是通过dialog来完成。

        class NamelessClass_1 implements Observer {            NamelessClass_1() {            }            @SuppressLint({"SyntheticAccessor"})            public void onChanged(LifecycleOwner lifecycleOwner) {                if (lifecycleOwner != null && DialogFragment.this.mShowsDialog) {                    View view = DialogFragment.this.requireView();                    if (view.getParent() != null) {                        throw new IllegalStateException("DialogFragment can not be attached to a container view");                    }                    if (DialogFragment.this.mDialog != null) {                        if (FragmentManager.isLoggingEnabled(3)) {Log.d("FragmentManager", "DialogFragment " + this + " setting the content view on " + DialogFragment.this.mDialog);                        }                        DialogFragment.this.mDialog.setContentView(view);                    }                }            }        }

 同样,在类中也提供了丰富的方法

1、显示

    public void show(@NonNull FragmentManager manager, @Nullable String tag) {        this.mDismissed = false;        this.mShownByMe = true;        FragmentTransaction ft = manager.beginTransaction();        ft.add(this, tag);        ft.commit();    }

2、消失

    public void dismiss() {        this.dismissInternal(false, false);    }

3、获取dialog

    @Nullable    public Dialog getDialog() {        return this.mDialog;    }

4、初始化内部资源

  @Nullable    View onFindViewById(int id) {        return this.mDialog != null ? this.mDialog.findViewById(id) : null;    }

5.dialog创建

    @MainThread    @NonNull    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {        if (FragmentManager.isLoggingEnabled(3)) {            Log.d("FragmentManager", "onCreateDialog called for DialogFragment " + this);        }        return new Dialog(this.requireContext(), this.getTheme());    }

三、实战

通过以上介绍,我们大概熟悉了DialogFragment的大概工作方式,如果想仔细研究,可以查看源码

在实战使用中

1、继承重写

基类:

abstract class BaseBindDialogFragment : DialogFragment() {    val TAG=javaClass::class.java.name    lateinit var bind: V    override fun onCreateView(        inflater: LayoutInflater,        container: ViewGroup?,        savedInstanceState: Bundle?    ): View? {        bind = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)        initView(bind.root)        return bind.root    }    override fun onStart() {        super.onStart()        initConfig(dialog)    }    @LayoutRes    abstract fun getLayoutId(): Int    abstract fun initView(view: View)    abstract fun initConfig(dialog:Dialog?)}
class MyDialogFrgament : BaseBindDialogFragment() {    override fun getLayoutId(): Int {        return R.layout.layout_dialog_view    }    override fun initView(view: View) {        bind.textInfo.text="我在测试"    }    override fun onResume() {        super.onResume()        MyLog.log(TAG,"onResume")    }    override fun onPause() {        super.onPause()        MyLog.log(TAG,"onPause")    }    override fun onStop() {        super.onStop()        MyLog.log(TAG,"onStop")    }    override fun initConfig(dialog: Dialog?) {        dialog?.let {        }    }}
                    

执行:

    private fun dialogExport() {        dialog = MyDialogFrgament()        dialog.show(supportFragmentManager, "dialogExport")    }

注意:

在开发过程中,好多新手会报错:

DialogFragment can not be attached to a container view

 

报错这个问题的原因是view的parent不为null,在消失后,又重新显示,这个时候会校验parent是否为null。

发生这个问题的核心是语法使用

解决:

获取view的parent,通过remove自己

每次展示的时候重新new一个对象

来源地址:https://blog.csdn.net/qq36246172/article/details/130558593

--结束END--

本文标题: Android Dialog之DialogFragment详解与使用

本文链接: https://www.lsjlt.com/news/393410.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作