MotionLayout 中的浮动操作按钮 (FAB),.show() 不起作用,.hide() 不正确
Floating action button(FAB) in MotionLayout, .show() doesn't work, .hide() work incorrect
我有带有 FragmentContainerView、BottomNavigationView 和 FAB 的 MainActivity。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/motionLayoutActivityMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".ui.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.fragment.app.FragmentContainerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/verticalGuidelineActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_percent="0.8" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/filterFabActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="fab"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/verticalGuidelineActivityMain" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/horizontalGuidelineActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="1"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>
当我点击名为 fab.show() 的 BottomNavifationView 的 item1 或 item2 时,如果我点击名为 fab.hide() 的 item3。
MainActivity.kt:
//SOME CODE BEFORE
binding.bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.membersFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(membersFragment).commit()
activeFragment = membersFragment
// Show fab when select membersFragment
binding.filterFabActivityMain.show()
true
}
R.id.teamsFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(teamsFragment).commit()
activeFragment = teamsFragment
// Show fab when select teamsFragment
binding.filterFabActivityMain.show()
true
}
R.id.aboutFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(aboutFragment).commit()
activeFragment = aboutFragment
// Hide fab when select aboutFragment
binding.filterFabActivityMain.hide()
true
}
else -> false
}
}
//SOME CODE AFTER
fab.hide() 隐藏 FAB,但如果我点击 FAB 位置 fab.clickListener 将被调用。
fab.show() 不会显示 FAB。
MotionLayout 控制场景,所以需要使用app:visibilityMode="ignore"
对运动场景中FAB 的约束
<Constraint
android:id="@+id/filterFabActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="fab"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/verticalGuidelineActivityMain"
app:visibilityMode="ignore" />
我有带有 FragmentContainerView、BottomNavigationView 和 FAB 的 MainActivity。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/motionLayoutActivityMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".ui.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.fragment.app.FragmentContainerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/verticalGuidelineActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_percent="0.8" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/filterFabActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="fab"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/verticalGuidelineActivityMain" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/horizontalGuidelineActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="1"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>
当我点击名为 fab.show() 的 BottomNavifationView 的 item1 或 item2 时,如果我点击名为 fab.hide() 的 item3。
MainActivity.kt:
//SOME CODE BEFORE
binding.bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.membersFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(membersFragment).commit()
activeFragment = membersFragment
// Show fab when select membersFragment
binding.filterFabActivityMain.show()
true
}
R.id.teamsFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(teamsFragment).commit()
activeFragment = teamsFragment
// Show fab when select teamsFragment
binding.filterFabActivityMain.show()
true
}
R.id.aboutFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(aboutFragment).commit()
activeFragment = aboutFragment
// Hide fab when select aboutFragment
binding.filterFabActivityMain.hide()
true
}
else -> false
}
}
//SOME CODE AFTER
fab.hide() 隐藏 FAB,但如果我点击 FAB 位置 fab.clickListener 将被调用。
fab.show() 不会显示 FAB。
MotionLayout 控制场景,所以需要使用app:visibilityMode="ignore"
对运动场景中FAB 的约束
<Constraint
android:id="@+id/filterFabActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="fab"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/verticalGuidelineActivityMain"
app:visibilityMode="ignore" />