gpt4 book ai didi

android - 如何将数据传递给 BottomSheetDialogFragment

转载 作者:行者123 更新时间:2023-12-04 14:46:05 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Send data from activity to fragment in Android

(22 个回答)


3年前关闭。




我在 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
}
}

最佳答案

使用 Bundle

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

ViewHolder类(class)
 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您的 BottomSheetFragment
arguments?.getString("key")
BottomSheetFragment类(class)
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
}

}

关于android - 如何将数据传递给 BottomSheetDialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56248633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com