【问题标题】:How to disable main activity FAB into another fragment?如何将主要活动 FAB 禁用到另一个片段中?
【发布时间】:2021-01-01 12:10:00
【问题描述】:

我有一个场景,我在主要活动中使用底部应用栏,这也是各种屏幕的 navhostfragment 和其他片段。

Navhost 片段(MainActivity)

在配方片段上显示一个工厂是可见的。问题是我只希望这个工厂在配方片段上,而不是在其他工厂上。在其他人身上,我只想要底部的应用栏。

如何在不为每个片段创建应用栏的情况下解决此问题?

ma​​inactivity xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabCradleMargin="10dp"
            app:fabCradleRoundedCornerRadius="10dp"
            app:fabCradleVerticalOffset="10dp">

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginEnd="16dp"
                android:background="@android:color/transparent"
                app:menu="@menu/bottom_appbar" />

        </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_baseline_add_24"
            app:layout_anchor="@id/bottomAppBar" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

当在底部应用程序栏中单击项目时,我设法显示不同的片段,但不想在配方片段以外的片段上显示 fab。

我正在使用 Kotlin,所以有一个解决方案会很有帮助。

【问题讨论】:

标签: android android-layout kotlin android-fragments frontend


【解决方案1】:

您可以通过在活动中调用addOnDestinationChangedListener 来监听来自NavController 类的目的地变化的一种方法是您的导航组件所在的位置,以下是在不同位置隐藏/显示FAB 的示例代码正在显示的片段:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        val fab = findViewById<FloatingActionButton>(R.id.fab)
        val bottomAppBar = findViewById<BottomAppBar>(R.id.bottomAppBar)


        // here is the part where you hide and show FAB
        navController.addOnDestinationChangedListener { _, destination, _ ->


            when ((destination as FragmentNavigator.Destination).className) {
                // show fab in recipe fragment
                RecipeFragment::class.qualifiedName -> {

                    fab.visibility = View.VISIBLE

                    // todo: set those values properly
                    bottomAppBar.fabCradleMargin = 30f
                    bottomAppBar.fabCradleRoundedCornerRadius = 30f
                    bottomAppBar.cradleVerticalOffset = 30f

                }
                // hide on other fragments
                else -> {
                    fab.visibility = View.GONE

                    bottomAppBar.fabCradleMargin = 0f
                    bottomAppBar.fabCradleRoundedCornerRadius = 0f
                    bottomAppBar.cradleVerticalOffset = 0f

                }
            }

        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多