Created
November 24, 2022 08:33
-
-
Save ibrahim-mrq/f33b76c26d65135ddc142984a4213cca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mrq.library.helpers | |
| import android.app.Dialog | |
| import android.os.Bundle | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.FrameLayout | |
| import androidx.annotation.LayoutRes | |
| import androidx.databinding.DataBindingUtil | |
| import androidx.databinding.ViewDataBinding | |
| import com.google.android.material.bottomsheet.BottomSheetBehavior | |
| import com.google.android.material.bottomsheet.BottomSheetDialog | |
| import com.google.android.material.bottomsheet.BottomSheetDialogFragment | |
| import java.lang.Exception | |
| import com.google.android.material.R | |
| class BaseBottomSheet( | |
| @LayoutRes private val layoutRes: Int = 0, | |
| private val style: Int, | |
| private val onBind: (dialog: Dialog, binding: ViewDataBinding) -> Unit, | |
| ) : BottomSheetDialogFragment() { | |
| private lateinit var binding: ViewDataBinding | |
| private lateinit var _dialog: Dialog | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| if (style != 0) setStyle(style, style) | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, | |
| container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View? { | |
| return try { | |
| if (layoutRes == 0) { | |
| dismiss() | |
| null | |
| } else { | |
| binding = DataBindingUtil.inflate(inflater, layoutRes, container, false) | |
| dialog?.setOnShowListener { dialog -> | |
| val sheetDialog = dialog as BottomSheetDialog | |
| val bottomSheet = | |
| sheetDialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout | |
| val behavior = BottomSheetBehavior.from(bottomSheet) | |
| behavior.state = BottomSheetBehavior.STATE_EXPANDED | |
| behavior.isFitToContents = false | |
| behavior.isDraggable = true | |
| behavior.isHideable = true | |
| } | |
| _dialog = dialog!! | |
| binding.root | |
| } | |
| } catch (e: Exception) { | |
| dismiss() | |
| null | |
| } | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| onBind(_dialog, binding) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun customBottomSheet() { | |
| val sheet = BaseBottomSheet( | |
| layoutRes = R.layout.dialog_alert, | |
| style = R.style.bottomSheetDialog, | |
| onBind = { dialog, binding -> | |
| val bind = binding as DialogAlertBinding | |
| bind.cancel.setOnClickListener { | |
| dialog.dismiss() | |
| } | |
| bind.ok.setOnClickListener { | |
| dialog.dismiss() | |
| } | |
| }) | |
| sheet.isCancelable = false | |
| sheet.Show(FragmentActivity().supportFragmentManager, "") | |
| // or | |
| changePasswordSheet.show(childFragmentManager, "") | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment