如何附加片段与对话框片段? [英] How to attach fragment with dialog fragment?

查看:53
本文介绍了如何附加片段与对话框片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码,该代码基本上是将AppCompatActivity附加到DialogFragment.我应该如何转换并将其用于将Fragment附加到DialogFragment.如果需要更多详细信息,请告诉我.

I using below code which was basically attaching the AppCompatActivity to DialogFragment. How should I convert and use it to attach Fragment to DialogFragment. If any more details are required please let me know.

@Override
public void onAttach(Context context) {
    super.onAttach(context);


    if (context instanceof FilterListener) {


        mFilterListener = (FilterListener) context;
    }
}

推荐答案

在显示DialogFragment之前,将其当前片段设置为targetFragment.

Before you will show your DialogFragment set your current fragment as a targetFragment for it.

dialog.setTargetFragment(this, getTargetRequestCode())

在您的DialogFragment实现中,覆盖onAttach(context: Context)方法:

In your DialogFragment implementation override the onAttach(context: Context) method:

override fun onAttach(context: Context) {
  super.onAttach(context)
  val targetFragment = this.targetFragment
  listener = if (targetFragment != null && targetFragment is YourDialogListener) {
    targetFragment
  } else if (context is YourDialogListener) {
    context
  } else {
    throw ClassCastException("$context must implement YourDialogListener.")
  }
}

这篇关于如何附加片段与对话框片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆