Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control Menu state using attributes #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bubbles/src/main/java/com/madrapps/bubbles/ActionMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -83,6 +104,7 @@ class ActionMenu @JvmOverloads constructor(
}
}
layout()
setMenuControlState()
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
Expand Down
1 change: 1 addition & 0 deletions bubbles/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<attr name="anchor" format="reference" />
<attr name="menu_layout" format="string" />
<attr name="menu_animator" format="string" />
<attr name="is_closed" format="boolean" />
</declare-styleable>
</resources>
11 changes: 3 additions & 8 deletions sample/src/main/java/com/madrapps/bubbles/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ class MainActivity : AppCompatActivity() {

val menu = findViewById<ActionMenu>(R.id.floatingActionMenu)

var flag = false
findViewById<View>(R.id.floatingActionButton).setOnClickListener {
flag = if (flag) {
menu.open()
false
} else {
menu.close()
true
}
menu.switchMenuState()
}
}


}
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton1"
Expand Down