-
-
Save nfaycel/b5f6dc48f938a9e90ca0258e7c6b7a29 to your computer and use it in GitHub Desktop.
This class is a generic class for BottomSheetDialogFragment using Kotlin, it will helps you to use one class for all ur bottom sheets on your app.
This file contains 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
/* | |
* Copyright (c) 2022. Qamar A. Safadi | |
*/ | |
package com.qamar.test.base | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.WindowManager | |
import android.widget.FrameLayout | |
import android.widget.Toast | |
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 com.qamar.test.R | |
open class BaseBottomSheet( | |
@LayoutRes private val layoutRes: Int? = 0, | |
private val corneredTheme: Int? = R.style.ThemeOverlay_MaterialComponents_BottomSheetDialog, | |
private val normalTheme: Int? = R.style.ThemeOverlay_MaterialComponents_BottomSheetDialog, | |
private val isCornered: Boolean? = false, | |
private val onBind: (View, binding: ViewDataBinding) -> Unit, | |
) : BottomSheetDialogFragment() { | |
//REMINDER : your layout must have data binding syntax <layout> </layout> to avoid null exception... | |
lateinit var binding: ViewDataBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
if (isCornered == true) { | |
setStyle(STYLE_NORMAL, corneredTheme!!) | |
} else setStyle(STYLE_NORMAL, normalTheme!!) | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
try { | |
return 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 bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) | |
setupHeight(bottomSheet) | |
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED | |
bottomSheetBehavior.isFitToContents = true | |
bottomSheetBehavior.isDraggable = true | |
bottomSheetBehavior.isHideable = true | |
} | |
binding.root | |
} | |
} catch (e: Exception) { | |
Toast.makeText( | |
context, | |
"Opps,You forget to convert ur layout to data binding layout ^_^", | |
Toast.LENGTH_LONG | |
).show() | |
return null | |
} | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
onBind(view, binding) | |
} | |
private fun setupHeight(bottomSheet: View) { | |
val layoutParams = bottomSheet.layoutParams | |
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT | |
bottomSheet.layoutParams = layoutParams | |
} | |
} |
This file contains 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
private fun bindChangePasswordSheet() { | |
val changePasswordSheet = BaseBottomSheet( | |
layoutRes = R.layout.change_password_bottom_sheet, | |
corneredTheme = R.style.AppBottomSheetDialogTheme, | |
isCornered = true, | |
onBind = { view, _binding -> | |
val bind = _binding as ChangePasswordBottomSheetBinding | |
with(bind) { | |
// do what you want | |
} | |
}) | |
changePasswordSheet.show(childFragmentManager, "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment