More helper methods in MenuSheetItemView

This commit is contained in:
Jays2Kings 2021-04-18 01:04:41 -04:00
parent 19ce2d2427
commit 3cf8b52025
2 changed files with 21 additions and 10 deletions

View File

@ -87,8 +87,7 @@ open class MaterialMenuSheet(
if (item.id == selectedId) { if (item.id == selectedId) {
currentIndex = index currentIndex = index
setIconColor(activity.getResourceColor(R.attr.colorAccent)) isSelected = true
setTextColor(activity.getResourceColor(R.attr.colorAccent))
} }
setOnClickListener { setOnClickListener {
@ -142,10 +141,7 @@ open class MaterialMenuSheet(
private fun clearEndDrawables() { private fun clearEndDrawables() {
(0 until binding.menuLayout.childCount).forEach { (0 until binding.menuLayout.childCount).forEach {
val itemView = (binding.menuLayout.getChildAt(it) as MenuSheetItemView) (binding.menuLayout.getChildAt(it) as? MenuSheetItemView)?.isSelected = false
itemView.setTextColor(primaryColor)
itemView.setIconColor(primaryColor)
itemView.setEndIcon(0)
} }
} }
@ -153,10 +149,7 @@ open class MaterialMenuSheet(
if (clearAll) { if (clearAll) {
clearEndDrawables() clearEndDrawables()
} }
val layout = binding.menuLayout.findViewById<MenuSheetItemView>(id) ?: return binding.menuLayout.findViewById<MenuSheetItemView>(id)?.selectWithEndIcon(drawableRes)
layout.setTextColor(layout.context.getResourceColor(R.attr.colorAccent))
layout.setIconColor(layout.context.getResourceColor(R.attr.colorAccent))
layout.setEndIcon(drawableRes)
} }
data class MenuSheetItem( data class MenuSheetItem(

View File

@ -13,6 +13,7 @@ import androidx.core.view.isGone
import androidx.core.widget.TextViewCompat import androidx.core.widget.TextViewCompat
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.MenuSheetItemBinding import eu.kanade.tachiyomi.databinding.MenuSheetItemBinding
import eu.kanade.tachiyomi.util.system.getResourceColor
class MenuSheetItemView @JvmOverloads constructor(context: Context, attrs: AttributeSet?) : class MenuSheetItemView @JvmOverloads constructor(context: Context, attrs: AttributeSet?) :
LinearLayout(context, attrs) { LinearLayout(context, attrs) {
@ -70,6 +71,23 @@ class MenuSheetItemView @JvmOverloads constructor(context: Context, attrs: Attri
text = context.getString(res) text = context.getString(res)
} }
fun selectWithEndIcon(@DrawableRes endDrawableRes: Int) {
isSelected = true
setEndIcon(endDrawableRes)
}
override fun setSelected(selected: Boolean) {
super.setSelected(selected)
if (isSelected) {
setIconColor(context.getResourceColor(R.attr.colorAccent))
setTextColor(context.getResourceColor(R.attr.colorAccent))
} else {
setTextColor(context.getResourceColor(android.R.attr.textColorPrimary))
setIconColor(context.getResourceColor(android.R.attr.textColorPrimary))
setEndIcon(0)
}
}
fun setTextColor(@ColorInt color: Int) { fun setTextColor(@ColorInt color: Int) {
binding?.itemTextView?.setTextColor(color) binding?.itemTextView?.setTextColor(color)
} }