Advertisement
babatunde360

BottomSheetDialog

Dec 10th, 2020
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.63 KB | None | 0 0
  1. class ExampleBottomSheetDialog : BottomSheetDialogFragment() {
  2.      lateinit var mListener:BottomSheetListener
  3.  
  4.     override fun onCreateView(
  5.         inflater: LayoutInflater,
  6.         container: ViewGroup?,
  7.         savedInstanceState: Bundle?
  8.     ): View {
  9.         val binding = FragmentSelectedItemListDialogItemBinding.inflate(inflater,container,false)
  10.         Log.d(TAG,"onCreateView")
  11.         Toast.makeText(requireContext(),"OnCreateView",Toast.LENGTH_SHORT).show()
  12.  
  13.         binding.button1.setOnClickListener {
  14.             mListener.onButtonClicked("Button 1 Clicked")
  15.             dismiss()
  16.         }
  17.         binding.button2.setOnClickListener {
  18.             mListener.onButtonClicked("Button 2 Clicked")
  19.             dismiss()
  20.         }
  21.  
  22.         return binding.root
  23.     }
  24.  
  25.  
  26.     override fun onAttachFragment(childFragment: Fragment) {
  27.         super.onAttachFragment(childFragment)
  28.         Log.d(TAG,"onAttachFragment")
  29.         Toast.makeText(requireContext(),"onAttachFragment",Toast.LENGTH_SHORT).show()
  30.         try{
  31.             mListener = childFragment as BottomSheetListener
  32.         }catch (e:ClassCastException){
  33.             throw ClassCastException("$childFragment must implement BottomSheetListener")
  34.         }
  35.     }
  36.  
  37.     interface BottomSheetListener{
  38.         fun onButtonClicked(text:String)
  39.     }
  40.     companion object{
  41.         const val TAG = "ExampleBottomSheetDialo"
  42.     }
  43. }
  44.  
  45.  
  46. #The Fragment that calls the BottomSheet
  47.  
  48.  
  49. class SelectedItemFragment : BottomSheetDialogFragment(),ExampleBottomSheetDialog.BottomSheetListener {
  50.     private lateinit var binding:FragmentSelectedItemListDialogBinding
  51.  
  52.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  53.                               savedInstanceState: Bundle?): View {
  54.          binding = FragmentSelectedItemListDialogBinding.inflate(inflater,container,false)
  55.  
  56.         binding.buttonOpenBottomSheet.setOnClickListener {
  57.             val bottomSheet = ExampleBottomSheetDialog()
  58.             bottomSheet.show(parentFragmentManager,"ExampleBottomSheet")
  59.         }
  60.  
  61.         return binding.root
  62.     }
  63.  
  64.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  65.  
  66.     }
  67.  
  68.  
  69.     companion object {
  70.  
  71.         // TODO: Customize parameters
  72.         fun newInstance(itemCount: Int): SelectedItemFragment =
  73.                 SelectedItemFragment().apply {
  74.                     arguments = Bundle().apply {
  75.                         putInt(ARG_ITEM_COUNT, itemCount)
  76.                     }
  77.                 }
  78.  
  79.     }
  80.  
  81.     override fun onButtonClicked(text: String) {
  82.         binding.textViewButtonClicked.text = text
  83.     }
  84. }
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement