Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ExampleBottomSheetDialog : BottomSheetDialogFragment() {
- lateinit var mListener:BottomSheetListener
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View {
- val binding = FragmentSelectedItemListDialogItemBinding.inflate(inflater,container,false)
- Log.d(TAG,"onCreateView")
- Toast.makeText(requireContext(),"OnCreateView",Toast.LENGTH_SHORT).show()
- binding.button1.setOnClickListener {
- mListener.onButtonClicked("Button 1 Clicked")
- dismiss()
- }
- binding.button2.setOnClickListener {
- mListener.onButtonClicked("Button 2 Clicked")
- dismiss()
- }
- return binding.root
- }
- override fun onAttachFragment(childFragment: Fragment) {
- super.onAttachFragment(childFragment)
- Log.d(TAG,"onAttachFragment")
- Toast.makeText(requireContext(),"onAttachFragment",Toast.LENGTH_SHORT).show()
- try{
- mListener = childFragment as BottomSheetListener
- }catch (e:ClassCastException){
- throw ClassCastException("$childFragment must implement BottomSheetListener")
- }
- }
- interface BottomSheetListener{
- fun onButtonClicked(text:String)
- }
- companion object{
- const val TAG = "ExampleBottomSheetDialo"
- }
- }
- #The Fragment that calls the BottomSheet
- class SelectedItemFragment : BottomSheetDialogFragment(),ExampleBottomSheetDialog.BottomSheetListener {
- private lateinit var binding:FragmentSelectedItemListDialogBinding
- override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?): View {
- binding = FragmentSelectedItemListDialogBinding.inflate(inflater,container,false)
- binding.buttonOpenBottomSheet.setOnClickListener {
- val bottomSheet = ExampleBottomSheetDialog()
- bottomSheet.show(parentFragmentManager,"ExampleBottomSheet")
- }
- return binding.root
- }
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
- }
- companion object {
- // TODO: Customize parameters
- fun newInstance(itemCount: Int): SelectedItemFragment =
- SelectedItemFragment().apply {
- arguments = Bundle().apply {
- putInt(ARG_ITEM_COUNT, itemCount)
- }
- }
- }
- override fun onButtonClicked(text: String) {
- binding.textViewButtonClicked.text = text
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement