如何将数据传递给BottomSheetDialogFragment [重复]

Posted

技术标签:

【中文标题】如何将数据传递给BottomSheetDialogFragment [重复]【英文标题】:How to pass data to BottomSheetDialogFragment [duplicate] 【发布时间】:2019-10-08 11:00:36 【问题描述】:

我在RecyclerView 中显示数据。当我点击列表项启动BottomSheetDialogFragment

如何将列表项数据传递给kotlin中的BottomSheetDialogFragment

 BottomSheetFragment().show(context!!.supportFragmentManager, "BottomSheetFragment")

class BottomSheetFragment() : BottomSheetDialogFragment() 

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? 
        var view = inflater.inflate(R.layout.coin_detail_lauout, container, false)
        return view
    

【问题讨论】:

【参考方案1】:

使用Bundle

    val bottomSheetFragment = BottomSheetFragment()
    val bundle = Bundle()
    bundle.putString("key", data)
    bottomSheetFragment.arguments = bundle

ViewHolder

 inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
    val textView = itemView.textView!!

    init 
        itemView.setOnClickListener 
            showBottomSheet(itemView.context, list.get(layoutPosition))
        
    

    private fun showBottomSheet(context: Context, data: String) 

        val bottomSheetFragment = BottomSheetFragment()
        val bundle = Bundle()
        bundle.putString("key", data)
        bottomSheetFragment.arguments = bundle

        bottomSheetFragment.show(
            (context as AppCompatActivity).supportFragmentManager,
            "bottomSheetFragment"
        )
    


在您的BottomSheetFragmentonCreateView

arguments?.getString("key")

BottomSheetFragment

class BottomSheetFragment : BottomSheetDialogFragment() 

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? 
    val view = inflater.inflate(R.layout.fragment_bottomsheetfragment_list_dialog, container, false)
    val data =  arguments?.getString("key")
//  Log.d("===",data+"")
    return view
    


【讨论】:

以上是关于如何将数据传递给BottomSheetDialogFragment [重复]的主要内容,如果未能解决你的问题,请参考以下文章

重新加载后如何将数据传递给局部视图

Ios:如何将数据传递给 UISplitViewController?

如何将数据传递给子 UINavigationController?

在准备 segue 时,如何将数据传递给子 UIViewController?

如何快速将数据传递给其他 UIViewcontroller

如何将数据传递给其他视图? [关闭]