使用 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"
)
}
}
在你的BottomSheetFragment
的onCreateView
方法中
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")
return view
}
}