我有以下情况:
我有NavigationDrawer和几个片段的活动,我可以在它们之间导航。该活动包括带有下拉日历的AppBarLayout。

内容活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content_main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main_screen">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemTextColor="@color/black"
app:itemIconTint="@color/backgroundScreens"
app:menu="@menu/bottom_navigation">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>应用程序栏:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:expanded="false">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingTop="?attr/actionBarSize"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="1.0">
<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:id="@+id/compactCalendarView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:compactCalendarBackgroundColor="@color/backgroundScreens"
app:compactCalendarTextSize="14sp"
app:compactCalendarTextColor="@color/white"
app:compactCalendarCurrentDayBackgroundColor="@color/white"
app:compactCalendarCurrentDayTextColor="@color/black"
app:compactCalendarCurrentSelectedDayBackgroundColor="@color/design_default_color_primary_dark"
/>
</LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:theme="@style/ToolbarTheme"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:background="@color/backgroundScreens">
<TextView
android:id="@+id/textViewToolbarTittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/relativeLayoutDatePickerButton"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="@+id/textViewDatePickerAppBarMainScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="@color/white"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageViewArrowAppBarMainScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@+id/textViewDatePickerAppBarMainScreen"
android:background="@drawable/ic_arrow_drop_down_calendar" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main_screen" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>和活动:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.diary.MainScreen">
<include
layout="@layout/app_bar_main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/main_screen_drawer">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>问题是,当我将一个片段更改为与图片中的片段不同的片段时。我想要更改工具栏和AppBarLayout。它看起来会完全不同,而不是延长日历。但仍然保持在片段之间导航的能力,这要归功于NavigationDrawer。我怎样才能做到这一点呢?
发布于 2019-12-23 23:03:11
您可以在设置onDestinationChangeListener的Activity或Fragment中添加nav_controller:
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.someFragment -> {
//change here
}
....
}
}https://stackoverflow.com/questions/59457042
复制相似问题