adding some bottom action menu related code style formatting
This commit is contained in:
parent
2cc4d857a4
commit
514081ee76
9 changed files with 37 additions and 67 deletions
|
@ -16,10 +16,10 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
|
|||
import com.simplemobiletools.commons.extensions.getContrastColor
|
||||
import com.simplemobiletools.commons.interfaces.MyActionModeCallback
|
||||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuCallback
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuItem
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuPopup
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView
|
||||
import java.util.*
|
||||
|
||||
abstract class MyRecyclerViewAdapter(val activity: BaseSimpleActivity, val recyclerView: MyRecyclerView, val itemClick: (Any) -> Unit) :
|
||||
|
|
|
@ -18,10 +18,10 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
|
|||
import com.simplemobiletools.commons.extensions.getContrastColor
|
||||
import com.simplemobiletools.commons.interfaces.MyActionModeCallback
|
||||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuCallback
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuItem
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuPopup
|
||||
import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView
|
||||
import java.util.*
|
||||
|
||||
abstract class MyRecyclerViewListAdapter<T>(
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.simplemobiletools.commons.views.bottomactionmenu
|
||||
|
||||
interface BottomActionMenuCallback {
|
||||
fun onItemClicked(item: BottomActionMenuItem){}
|
||||
fun onViewCreated(view: BottomActionMenuView){}
|
||||
fun onViewDestroyed(){}
|
||||
fun onItemClicked(item: BottomActionMenuItem) {}
|
||||
fun onViewCreated(view: BottomActionMenuView) {}
|
||||
fun onViewDestroyed() {}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,10 @@ import android.content.Context
|
|||
import android.graphics.Rect
|
||||
import android.view.*
|
||||
import android.view.View.MeasureSpec
|
||||
import android.widget.*
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.ListView
|
||||
import android.widget.PopupWindow
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.widget.PopupWindowCompat
|
||||
import com.simplemobiletools.commons.R
|
||||
|
@ -13,14 +16,12 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
|
|||
import com.simplemobiletools.commons.extensions.windowManager
|
||||
import com.simplemobiletools.commons.helpers.isRPlus
|
||||
import kotlinx.android.synthetic.main.item_action_mode_popup.view.*
|
||||
import kotlin.math.ceil
|
||||
|
||||
class BottomActionMenuItemPopup(
|
||||
private val context: Context,
|
||||
private val items: List<BottomActionMenuItem>,
|
||||
private val onSelect: (BottomActionMenuItem) -> Unit
|
||||
) {
|
||||
|
||||
private val popup = PopupWindow(context, null, android.R.attr.popupMenuStyle)
|
||||
private var anchorView: View? = null
|
||||
private var dropDownWidth = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
|
@ -41,6 +42,7 @@ class BottomActionMenuItemPopup(
|
|||
if (view == null) {
|
||||
view = LayoutInflater.from(context).inflate(R.layout.item_action_mode_popup, parent, false)
|
||||
}
|
||||
|
||||
val item = items[position]
|
||||
view!!.cab_item.text = item.title
|
||||
if (item.icon != View.NO_ID) {
|
||||
|
@ -48,10 +50,12 @@ class BottomActionMenuItemPopup(
|
|||
icon?.applyColorFilter(context.getAdjustedPrimaryColor())
|
||||
view.cab_item.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
|
||||
}
|
||||
|
||||
view.setOnClickListener {
|
||||
onSelect.invoke(item)
|
||||
popup.dismiss()
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +75,8 @@ class BottomActionMenuItemPopup(
|
|||
PopupWindowCompat.setWindowLayoutType(popup, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL)
|
||||
popup.isOutsideTouchable = true
|
||||
if (popup.isShowing) {
|
||||
popup.update(anchorView, dropDownHorizontalOffset, dropDownVerticalOffset, dropDownWidth, if (dropDownHeight < 0) -1 else dropDownHeight)
|
||||
val dropDownHeight = if (dropDownHeight < 0) -1 else dropDownHeight
|
||||
popup.update(anchorView, dropDownHorizontalOffset, dropDownVerticalOffset, dropDownWidth, dropDownHeight)
|
||||
} else {
|
||||
popup.width = dropDownWidth
|
||||
popup.height = dropDownHeight
|
||||
|
@ -84,14 +89,6 @@ class BottomActionMenuItemPopup(
|
|||
popup.contentView = null
|
||||
}
|
||||
|
||||
internal fun setOnDismissListener(listener: (() -> Unit)?) {
|
||||
if (listener != null) {
|
||||
popup.setOnDismissListener { listener.invoke() }
|
||||
} else {
|
||||
popup.setOnDismissListener(null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildDropDown() {
|
||||
var otherHeights = 0
|
||||
val dropDownList = ListView(context).apply {
|
||||
|
@ -132,7 +129,7 @@ class BottomActionMenuItemPopup(
|
|||
padding = 0
|
||||
}
|
||||
|
||||
if ((dropDownGravity and Gravity.BOTTOM) == Gravity.BOTTOM) {
|
||||
if (dropDownGravity and Gravity.BOTTOM == Gravity.BOTTOM) {
|
||||
dropDownVerticalOffset += anchorView!!.height
|
||||
}
|
||||
|
||||
|
@ -161,7 +158,6 @@ class BottomActionMenuItemPopup(
|
|||
* @see androidx.appcompat.widget.DropDownListView.measureHeightOfChildrenCompat
|
||||
*/
|
||||
private fun measureHeightOfChildrenCompat(maxHeight: Int): Int {
|
||||
|
||||
val parent = FrameLayout(context)
|
||||
val widthMeasureSpec = MeasureSpec.makeMeasureSpec(dropDownWidth, MeasureSpec.EXACTLY)
|
||||
|
||||
|
@ -182,27 +178,27 @@ class BottomActionMenuItemPopup(
|
|||
|
||||
// Compute child height spec
|
||||
val heightMeasureSpec: Int
|
||||
var childLp: ViewGroup.LayoutParams? = child.layoutParams
|
||||
var childLayoutParams: ViewGroup.LayoutParams? = child.layoutParams
|
||||
|
||||
if (childLp == null) {
|
||||
childLp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
child.layoutParams = childLp
|
||||
if (childLayoutParams == null) {
|
||||
childLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
child.layoutParams = childLayoutParams
|
||||
}
|
||||
|
||||
heightMeasureSpec = if (childLp.height > 0) {
|
||||
heightMeasureSpec = if (childLayoutParams.height > 0) {
|
||||
MeasureSpec.makeMeasureSpec(
|
||||
childLp.height,
|
||||
childLayoutParams.height,
|
||||
MeasureSpec.EXACTLY
|
||||
)
|
||||
} else {
|
||||
MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
|
||||
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
|
||||
}
|
||||
child.measure(widthMeasureSpec, heightMeasureSpec)
|
||||
// Since this view was measured directly against the parent measure
|
||||
// spec, we must measure it again before reuse.
|
||||
child.forceLayout()
|
||||
|
||||
val marginLayoutParams = childLp as? ViewGroup.MarginLayoutParams
|
||||
val marginLayoutParams = childLayoutParams as? ViewGroup.MarginLayoutParams
|
||||
val topMargin = marginLayoutParams?.topMargin ?: 0
|
||||
val bottomMargin = marginLayoutParams?.bottomMargin ?: 0
|
||||
val verticalMargin = topMargin + bottomMargin
|
||||
|
|
|
@ -74,6 +74,12 @@ internal class BottomActionMenuParser(private val context: Context) {
|
|||
val visible = typedArray.getBoolean(R.styleable.BottomActionMenuItem_android_visible, true)
|
||||
typedArray.recycle()
|
||||
parser.require(XmlPullParser.START_TAG, null, MENU_ITEM_TAG)
|
||||
return BottomActionMenuItem(id, text, iconId, showAsAction == MenuItem.SHOW_AS_ACTION_ALWAYS || showAsAction == MenuItem.SHOW_AS_ACTION_IF_ROOM, visible)
|
||||
return BottomActionMenuItem(
|
||||
id,
|
||||
text,
|
||||
iconId,
|
||||
showAsAction == MenuItem.SHOW_AS_ACTION_ALWAYS || showAsAction == MenuItem.SHOW_AS_ACTION_IF_ROOM,
|
||||
visible
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,14 +10,12 @@ import androidx.core.widget.PopupWindowCompat
|
|||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||
|
||||
|
||||
class BottomActionMenuPopup(private val activity: BaseSimpleActivity, items: List<BottomActionMenuItem>) {
|
||||
private val bottomActionMenuView = BottomActionMenuView(activity)
|
||||
private val popup = PopupWindow(activity, null, android.R.attr.popupMenuStyle)
|
||||
private var floatingActionButton: FloatingActionButton? = null
|
||||
private var callback: BottomActionMenuCallback? = null
|
||||
|
||||
|
||||
constructor(activity: BaseSimpleActivity, @MenuRes menuResId: Int) : this(activity, BottomActionMenuParser(activity).inflate(menuResId))
|
||||
|
||||
init {
|
||||
|
|
|
@ -11,18 +11,13 @@ import android.view.View
|
|||
import android.view.ViewPropertyAnimator
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.annotation.MenuRes
|
||||
import androidx.annotation.StringRes
|
||||
import com.google.android.material.animation.AnimationUtils
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.isRPlus
|
||||
|
||||
|
||||
class BottomActionMenuView : LinearLayout {
|
||||
|
||||
companion object {
|
||||
private const val ENTER_ANIMATION_DURATION = 225
|
||||
private const val EXIT_ANIMATION_DURATION = 175
|
||||
|
@ -37,7 +32,12 @@ class BottomActionMenuView : LinearLayout {
|
|||
private val inflater = LayoutInflater.from(context)
|
||||
private val itemsLookup = LinkedHashMap<Int, BottomActionMenuItem>()
|
||||
private val items: List<BottomActionMenuItem>
|
||||
get() = itemsLookup.values.toList().sortedWith(compareByDescending<BottomActionMenuItem> { it.showAsAction }.thenBy { it.icon != View.NO_ID }).filter { it.isVisible }
|
||||
get() = itemsLookup.values.toList().sortedWith(compareByDescending<BottomActionMenuItem> {
|
||||
it.showAsAction
|
||||
}.thenBy {
|
||||
it.icon != View.NO_ID
|
||||
}).filter { it.isVisible }
|
||||
|
||||
private var currentAnimator: ViewPropertyAnimator? = null
|
||||
private var callback: BottomActionMenuCallback? = null
|
||||
|
||||
|
@ -99,14 +99,6 @@ class BottomActionMenuView : LinearLayout {
|
|||
})
|
||||
}
|
||||
|
||||
fun createFromMenu(@MenuRes menuResId: Int) {
|
||||
if (menuResId != View.NO_ID) {
|
||||
val menuParser = BottomActionMenuParser(context)
|
||||
val items = menuParser.inflate(menuResId)
|
||||
setup(items)
|
||||
}
|
||||
}
|
||||
|
||||
fun setup(items: List<BottomActionMenuItem>) {
|
||||
items.forEach { itemsLookup[it.id] = it }
|
||||
init()
|
||||
|
@ -116,25 +108,6 @@ class BottomActionMenuView : LinearLayout {
|
|||
setItem(item)
|
||||
}
|
||||
|
||||
fun setItemShowAsAction(@IdRes itemId: Int, showAsAction: Boolean) {
|
||||
val item = itemsLookup[itemId]
|
||||
setItem(item?.copy(showAsAction = showAsAction))
|
||||
}
|
||||
|
||||
fun changeItemIcon(@IdRes itemId: Int, @DrawableRes iconRes: Int) {
|
||||
val item = itemsLookup[itemId]
|
||||
setItem(item?.copy(icon = iconRes))
|
||||
}
|
||||
|
||||
fun changeItemTitle(@IdRes itemId: Int, @StringRes stringRes: Int) {
|
||||
changeItemTitle(itemId, context.getString(stringRes))
|
||||
}
|
||||
|
||||
fun changeItemTitle(@IdRes itemId: Int, title: String) {
|
||||
val item = itemsLookup[itemId]
|
||||
setItem(item?.copy(title = title))
|
||||
}
|
||||
|
||||
private fun setItem(item: BottomActionMenuItem?) {
|
||||
item?.let {
|
||||
itemsLookup[item.id] = item
|
||||
|
|
|
@ -12,4 +12,4 @@
|
|||
android:textSize="@dimen/bigger_text_size"
|
||||
tools:drawableStart="@drawable/ic_rename_vector"
|
||||
tools:drawableTint="@color/color_primary"
|
||||
tools:text="@tools:sample/cities" />
|
||||
tools:text="@string/delete" />
|
||||
|
|
|
@ -20,8 +20,6 @@
|
|||
<dimen name="colorpicker_color_width">70dp</dimen>
|
||||
|
||||
<dimen name="normal_icon_size">48dp</dimen>
|
||||
<dimen name="fastscroll_width">8dp</dimen>
|
||||
<dimen name="fastscroll_height">40dp</dimen>
|
||||
<dimen name="fingerprint_icon_size">72dp</dimen>
|
||||
<dimen name="fab_size">56dp</dimen>
|
||||
<dimen name="secondary_fab_bottom_margin">76dp</dimen>
|
||||
|
@ -51,7 +49,6 @@
|
|||
<dimen name="actionbar_text_size">20sp</dimen>
|
||||
<dimen name="extra_big_text_size">22sp</dimen>
|
||||
|
||||
<dimen name="cab_popup_menu_max_width">336dp</dimen>
|
||||
<dimen name="cab_popup_menu_min_width">168dp</dimen>
|
||||
<dimen name="cab_item_min_width">85dp</dimen>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue