7

I am showing data in RecyclerView. When i click on list item launching BottomSheetDialogFragment.

How to pass list item data to BottomSheetDialogFragment in kotlin

 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
    }
}
ADM
  • 20,406
  • 11
  • 52
  • 83
Techchai Mobile
  • 91
  • 1
  • 1
  • 3

1 Answers1

17

use Bundle

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

on 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"
        )
    }

}

inside onCreateView of your 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
    }

}
shb
  • 5,957
  • 2
  • 15
  • 32