#android #kotlin #floating-action-button #bottomnavigationview #material-components-android
#Android #котлин #плавающая кнопка действия #вид нижней навигации #material-components-android
Вопрос:
У меня есть MainActivity с FragmentContainerView, BottomNavigationView и FAB.
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>
Когда я нажимаю на item1 или item2 BottomNavifationView, вызываемый fab.show(), если я нажимаю на item3, вызываемый fab.hide() .
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.
Ответ №1:
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" />