如何将数据从片段传递到DialogFragment

时间:2019-03-15 21:55:51

标签: android android-fragments kotlin android-adapter android-dialogfragment

我有一个Fragment,其中有2个ArrayList,其中包含Strings,我想从Fragment传递,以在DialogFragment中显示一些信息。但是,我目前不知道应该使用什么代码来实现此目标。我尝试将private val myListTitles: ArrayList<String>, private val myListDescriptions: ArrayList<String>添加到片段类的构造函数中,但这没有用。实现for循环并获取每个项目的位置而不是对每个项目进行1比1硬编码的正确方法是什么?

片段类

class MyFragment : androidx.fragment.app.Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.my_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        val myListTitles = ArrayList<String>()
        myListTitles.add("Tab A")
        myListTitles.add("Tab B")
        myListTitles.add("Tab C")

        val myListDescriptions = ArrayList<String>()
        myListDescriptions.add("Description A")
        myListDescriptions.add("Description B")
        myListDescriptions.add("Description C")


        super.onActivityCreated(savedInstanceState)
    }
}

DialogFragment类

class TabbedDialog : DialogFragment(private val myListTitles: ArrayList<String>, private val myListDescriptions: ArrayList<String>) {
    lateinit var customView: View

    private var myListTitles = myList.toMutableList()
    private var myListDescriptions = myList.toMutableList()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return customView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        val b = AlertDialog.Builder(activity)
                .setTitle("Dialog Title")
                .setPositiveButton("OK") { dialog, _ -> dialog.dismiss() }

        customView = activity!!.layoutInflater.inflate(R.layout.fragment_dialog, null)

        val tabLayout = customView.findViewById<TabLayout>(R.id.mTabLayout)
        val viewPager = customView.findViewById<ViewPager>(R.id.mViewPager)

        val adapter = TabbedDialogAdapter(childFragmentManager)
        adapter.addFragment("Tab A", TabbedDialogFragment.createInstance("Description A"))
        adapter.addFragment("Tab B", TabbedDialogFragment.createInstance("Description B"))
        adapter.addFragment("Tab C", TabbedDialogFragment.createInstance("Description C"))

        val adapter = TabbedDialogAdapter(childFragmentManager)
        for (item in myList) {
            // ¿What goes here?

            // My guess
            // adapter.addFragment(myListTitles[position], TabbedDialogFragment.createInstance(myListDescriptions[position]))
        }

        viewPager.adapter = adapter
        tabLayout.setupWithViewPager(viewPager)

        b.setView(customView)
        return b.create()
    }
}

2 个答案:

答案 0 :(得分:0)

将数据从一个片段传递到另一个片段的规范方法是创建一个静态工厂方法,然后通过片段arguments捆绑包传递参数,如下所示:

class MyDialogFragment : DialogFragment() {

    private lateinit var items: List<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        items = arguments?.getStringArrayList(ITEMS) ?: throw IllegalStateException("No args provided")
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        /* omitted */
    }

    companion object {

        private const val ITEMS = "items"

        fun newInstance(
                items: ArrayList<String>
        ): MyDialogFragment = MyDialogFragment().apply {
            arguments = Bundle().apply {
                putStringArrayList(ITEMS, items)
            }
        }
    }

}

编辑:

在您的片段中,您可以这样称呼它

val dialog = MyDialogFragment.newInstance(items)
dialog.show(childFragmentManager, "dialog")

答案 1 :(得分:-1)

您可以使用事件总线,实时数据或通过注入单例来管理数据来传递它们,然后可以从任何位置(片段,活动,对话框等)读取它们。 如果数据不会改变,您也可以将它们作为公共静态最终变量放在任何类上,并从任何地方使用它