当前位置: 首页 > 知识库问答 >
问题:

如何在每次片段目标更改时展开AppBarLayout/Toolbar

臧正平
2023-03-14

我正在建设与许多碎片的单一活动应用程序,我正在使用Jetpack导航组件。我在工具栏中有app:layout_scrollflags=“scrollenteralways”,这样当用户滚动内容时,工具栏就可以滚动,可以提供很大的空间,它可以正常工作。
但是在一个片段中,我有SearchView in Action Bar菜单,所以当用户从具有可滚动内容的片段导航到这个搜索片段时,SearchView和其他菜单项将自动隐藏。如何在创建/显示每个片段时使ActionBar/ToolBar可见。

我的活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activities.MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_nav">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <include layout="@layout/content_main" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@android:color/white"
        app:menu="@menu/main_bottom" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.fragments.notification.NotificationsFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

共有1个答案

燕璞
2023-03-14

很简单。我通过在NavController上添加侦听器解决了它

        navController.addOnDestinationChangedListener { _, _, _ ->
            appBarLayout.setExpanded(true, true)
        }
 类似资料:
  • 我有一个片段,它添加了一个选项到选项菜单。当单击此选项时,将打开一个对话框片段。对话框将原始片段设置为其目标片段。如果在对话片段打开时没有发生方向变化,则目标片段与预期的一样,但是在方向变化之后,目标片段被设置为对话片段本身,而不是先前设置的片段。结果,当试图将目标片段强制转换为被设置为目标片段的片段时,我得到了一个classCastException。我需要在对话框中获取目标片段,因为它实现了一

  • 我有一个片段使用了新的CoordinatorLayout/AppBarLayout/CollapsingToolBarLayout范例,我希望能够检测折叠工具栏何时完全展开,以便对它所在的整个片段执行操作,例如从堆栈中弹出片段并转到新的堆栈中,删除该片段。我有解散的代码工作,我只需要知道什么时候和什么时候不使用它。 提前感谢! 编辑:我还看到AppBarlayout.SetExpanded(...

  • 我正在开发一个。能让它一变就重启吗?我正在Coffeescript中开发它。是否可以监视以便在保存更改时重新启动?

  • 我想每次改变整数变量时发送更新

  • 问题内容: 我想将自己更改为我尝试过的内容,但无法将我的活动更改为片段。有人可以告诉我该怎么做吗?在哪里编辑我是Android上的新手。请告诉我和在此先感谢 这是我的活动: 问题答案: 只要了解一些步骤,您就可以现在和将来轻松地将Activity转换为Fragment ..: 首先,不要扩展,只要扩展.. 例如: 然后覆盖.. 然后通过LayoutInflater扩展布局,并分配给进一步用于子视图

  • StaticPagerAdapter dummyfragment1 layoutfragment1