Menu bottom sheet now scrolls to the active menu item if list is too tall

This commit is contained in:
Jay 2020-05-18 23:43:15 -04:00
parent 83891c9b6b
commit 0b5a69d9c3

View File

@ -41,7 +41,7 @@ class MaterialMenuSheet(
BottomSheetDialog BottomSheetDialog
(activity, R.style.BottomSheetDialogTheme) { (activity, R.style.BottomSheetDialogTheme) {
val primaryColor = activity.getResourceColor(android.R.attr.textColorPrimary) private val primaryColor = activity.getResourceColor(android.R.attr.textColorPrimary)
private val view = activity.layoutInflater.inflate(R.layout.bottom_menu_sheet, null) private val view = activity.layoutInflater.inflate(R.layout.bottom_menu_sheet, null)
init { init {
@ -51,12 +51,13 @@ class MaterialMenuSheet(
window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} }
items.forEach { var currentIndex: Int? = null
items.forEachIndexed { index, item ->
val view = val view =
activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as ViewGroup activity.layoutInflater.inflate(R.layout.menu_sheet_item, null) as ViewGroup
val textView = view.getChildAt(0) as MaterialTextView val textView = view.getChildAt(0) as MaterialTextView
with(view) { with(view) {
id = it.id id = item.id
menu_layout.addView(this) menu_layout.addView(this)
setOnClickListener { setOnClickListener {
val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id) val shouldDismiss = onMenuItemClicked(this@MaterialMenuSheet, id)
@ -66,13 +67,14 @@ class MaterialMenuSheet(
} }
} }
with(textView) { with(textView) {
if (it.text != null) { if (item.text != null) {
text = it.text text = item.text
} else { } else {
setText(it.textRes) setText(item.textRes)
} }
setCompoundDrawablesRelativeWithIntrinsicBounds(it.drawable, 0, 0, 0) setCompoundDrawablesRelativeWithIntrinsicBounds(item.drawable, 0, 0, 0)
if (it.id == selectedId) { if (item.id == selectedId) {
currentIndex = index
setTextColorRes(R.color.colorAccent) setTextColorRes(R.color.colorAccent)
compoundDrawableTintList = compoundDrawableTintList =
ColorStateList.valueOf(context.getColor(R.color.colorAccent)) ColorStateList.valueOf(context.getColor(R.color.colorAccent))
@ -92,6 +94,12 @@ class MaterialMenuSheet(
title_layout.visibleIf(title != null) title_layout.visibleIf(title != null)
toolbar_title.text = title toolbar_title.text = title
currentIndex?.let {
view.post {
menu_scroll_view.scrollTo(0, it * 48.dpToPx - menu_scroll_view.height / 2)
}
}
var isNotElevated = false var isNotElevated = false
var elevationAnimator: ValueAnimator? = null var elevationAnimator: ValueAnimator? = null
if (title_layout.isVisible()) { if (title_layout.isVisible()) {