这个问题已经被问过几次了,但我们现在是 2020 年,有没有人找到一个很好的可用解决方案呢?
我希望能够使用底部导航控件进行导航,而无需在每次选择片段时刷新它们。这是我目前所拥有的:
导航/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/main"
app:startDestination="@id/home">
<fragment
android:id="@+id/home"
android:name="com.org.ftech.fragment.HomeFragment"
android:label="@string/app_name"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/news"
android:name="com.org.ftech.fragment.NewsFragment"
android:label="News"
tools:layout="@layout/fragment_news"/>
<fragment
android:id="@+id/markets"
android:name="com.org.ftech.fragment.MarketsFragment"
android:label="Markets"
tools:layout="@layout/fragment_markets"/>
<fragment
android:id="@+id/explore"
android:name="com.org.ftech.ExploreFragment"
android:label="Explore"
tools:layout="@layout/fragment_explore"/>
</navigation>
Run Code Online (Sandbox Code Playgroud)
活动邮件.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="@color/nav"
app:itemTextColor="@color/nav"
app:layout_constraintBottom_toBottomOf="parent" …Run Code Online (Sandbox Code Playgroud)