如何将数据传递到BottomSheetDialogFragment

时间:2019-05-22 02:47:50

标签: android kotlin

我正在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 个答案:

答案 0 :(得分:0)

使用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"
        )
    }

}

onCreateView的{​​{1}}内部

BottomSheetFragment

arguments?.getString("key")

BottomSheetFragment