Last active
April 30, 2021 03:58
-
-
Save DanteAndroid/9d7b02edef362a0f5f011b8148f94bb0 to your computer and use it in GitHub Desktop.
Easy-to-use bottom dialogs
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
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.annotation.LayoutRes | |
import androidx.annotation.StyleRes | |
import com.google.android.material.bottomsheet.BottomSheetDialogFragment | |
import com.rrtv.ui.R | |
/** | |
* @author Dante | |
* 2021/3/9 | |
*/ | |
class BottomDialog private constructor( | |
@LayoutRes val layoutRes: Int, | |
@StyleRes val styleRes: Int | |
) : BottomSheetDialogFragment() { | |
// <style name="BottomSheetDialog" parent="Theme.MaterialComponents.Dialog"> | |
// <item name="android:windowBackground">@android:color/transparent</item> | |
// <item name="android:windowAnimationStyle"> | |
// @style/Animation.MaterialComponents.BottomSheetDialog | |
// </item> | |
// </style> | |
private lateinit var listener: OnViewCreatedListener | |
companion object { | |
fun newInstance( | |
@LayoutRes layoutRes: Int = R.layout.dialog_common, | |
@StyleRes styleRes: Int = R.style.BottomSheetDialog | |
): BottomDialog { | |
return BottomDialog(layoutRes, styleRes) | |
} | |
} | |
fun setOnViewCreated(listener: OnViewCreatedListener) { | |
this.listener = listener | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setStyle(STYLE_NORMAL, styleRes) | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
val view = inflater.inflate(layoutRes, container, false) | |
view.findViewById<View>(R.id.cancel)?.setOnClickListener { | |
dismissAllowingStateLoss() | |
} | |
if (this::listener.isInitialized) { | |
listener.onViewCreated(view) | |
} | |
return view | |
} | |
interface OnViewCreatedListener { | |
fun onViewCreated(view: View) | |
} | |
} |
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
import android.app.Dialog | |
import android.os.Bundle | |
import android.view.* | |
import androidx.annotation.LayoutRes | |
import androidx.annotation.StyleRes | |
import androidx.fragment.app.DialogFragment | |
import com.same.android.R | |
abstract class BottomDialogFragment : DialogFragment() { | |
@LayoutRes | |
abstract fun getLayoutResId(): Int | |
@StyleRes | |
open fun getDialogStyle(): Int = R.style.BottomDialog | |
open fun getDimAmount(): Float = 0.6f | |
open fun getHeight() :Int = ViewGroup.LayoutParams.WRAP_CONTENT | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
return Dialog(requireContext(), getDialogStyle()).apply { | |
window?.let { | |
it.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT) | |
it.setBackgroundDrawableResource(android.R.color.transparent) | |
it.setDimAmount(getDimAmount()) | |
val wlp = it.attributes | |
wlp.gravity = Gravity.BOTTOM | |
it.attributes = wlp | |
} | |
} | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
return inflater.inflate(getLayoutResId(), container, false) | |
} | |
override fun onStart() { | |
super.onStart() | |
dialog?.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT, getHeight()) | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<style name="BottomDialog" parent="Theme.AppCompat.Dialog"> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
<!-- <item name="android:windowTranslucentNavigation">true</item> --> // 打开可优化在有虚拟键手机上的动画效果 | |
<item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item> | |
</style> | |
<style name="BottomDialogAnimation"> | |
<item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item> | |
<item name="android:windowExitAnimation">@anim/slide_out_to_bottom</item> | |
</style> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment