From 8caaf620813c7fd1a8425f94434d9488e8484c31 Mon Sep 17 00:00:00 2001 From: Murali Krishnan Date: Tue, 8 Jan 2019 15:13:11 +0530 Subject: [PATCH] Control Menu state using attributes --- .../java/com/madrapps/bubbles/ActionMenu.kt | 22 +++++++++++++++++++ bubbles/src/main/res/values/attrs.xml | 1 + .../madrapps/bubbles/sample/MainActivity.kt | 11 +++------- sample/src/main/res/layout/activity_main.xml | 3 ++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/bubbles/src/main/java/com/madrapps/bubbles/ActionMenu.kt b/bubbles/src/main/java/com/madrapps/bubbles/ActionMenu.kt index b3a8a89..62fd538 100644 --- a/bubbles/src/main/java/com/madrapps/bubbles/ActionMenu.kt +++ b/bubbles/src/main/java/com/madrapps/bubbles/ActionMenu.kt @@ -18,6 +18,7 @@ class ActionMenu @JvmOverloads constructor( private lateinit var layout: Layout private lateinit var animator: Animator + private var isMenuClosed = false private val parentPosition = Rect() private val anchorPosition = Rect(0, 0, 0, 0) @@ -32,6 +33,7 @@ class ActionMenu @JvmOverloads constructor( anchorId = getResourceId(R.styleable.ActionMenu_anchor, -1) setLayout(getString(R.styleable.ActionMenu_menu_layout)) setAnimator(getString(R.styleable.ActionMenu_menu_animator)) + setDefaultMenuState(getBoolean(R.styleable.ActionMenu_is_closed,false)) recycle() } } @@ -50,6 +52,20 @@ class ActionMenu @JvmOverloads constructor( } } + private fun setDefaultMenuState(isMenuClosed: Boolean?){ + if (isMenuClosed != null) { + this.isMenuClosed = isMenuClosed + } + } + + private fun setMenuControlState(){ + if(this.isMenuClosed){ + this.close() + }else{ + this.open() + } + } + private fun getClazz(classString: String?): Class<*> { try { return javaClass.classLoader.loadClass(classString) @@ -58,6 +74,11 @@ class ActionMenu @JvmOverloads constructor( } } + fun switchMenuState(){ + this.isMenuClosed = !this.isMenuClosed + setMenuControlState() + } + override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { parentPosition.setEmpty() anchorPosition.setEmpty() @@ -83,6 +104,7 @@ class ActionMenu @JvmOverloads constructor( } } layout() + setMenuControlState() } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { diff --git a/bubbles/src/main/res/values/attrs.xml b/bubbles/src/main/res/values/attrs.xml index 655fd73..eeac05c 100644 --- a/bubbles/src/main/res/values/attrs.xml +++ b/bubbles/src/main/res/values/attrs.xml @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/sample/src/main/java/com/madrapps/bubbles/sample/MainActivity.kt b/sample/src/main/java/com/madrapps/bubbles/sample/MainActivity.kt index b070f91..19ef67a 100644 --- a/sample/src/main/java/com/madrapps/bubbles/sample/MainActivity.kt +++ b/sample/src/main/java/com/madrapps/bubbles/sample/MainActivity.kt @@ -13,15 +13,10 @@ class MainActivity : AppCompatActivity() { val menu = findViewById(R.id.floatingActionMenu) - var flag = false findViewById(R.id.floatingActionButton).setOnClickListener { - flag = if (flag) { - menu.open() - false - } else { - menu.close() - true - } + menu.switchMenuState() } } + + } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 9c762c4..981fe7c 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -30,7 +30,8 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:menu_animator="com.madrapps.bubbles.animators.VerticalStackedAnimator" - app:menu_layout="com.madrapps.bubbles.layouts.VerticalStackedLayout"> + app:menu_layout="com.madrapps.bubbles.layouts.VerticalStackedLayout" + app:is_closed="true">