簡體   English   中英

具有導航編輯器的BottomNavigationView面臨片段未更改的問題

[英]BottomNavigationView with Navigation Editor facing issue of fragment not changing

我正在嘗試將BottomNavigationView與Navigation Editor一起使用。 我已經實現了給定的一切,但是只顯示了主頁,當我更改選項卡時,它不會更改片段。

這是主要的活動代碼:

public class MainActivity extends AppCompatActivity {
    BottomNavigationView mainBottomNavigation;

    NavController navController;
    NavHostFragment navHostFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainBottomNavigation = findViewById(R.id.bottom_navigation);
        navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(mainBottomNavigation, Navigation.findNavController(this,R.id.nav_host_fragment));
    } }

這是主要的活動xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

這是菜單文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_location"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_location" />

    <item
        android:id="@+id/navigation_sightseeing"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_sightseeing" />

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_gallery"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_gallery" />

    <item
        android:id="@+id/navigation_contact_us"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_contact_us" />

</menu>

這是我創建的nav_graph:

<?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/nav_graph"
    app:startDestination="@id/navigation_home">
    <fragment
        android:id="@+id/navigation_location"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_location"
        tools:layout="@layout/fragment_location" />
    <fragment
        android:id="@+id/navigation_sightseeing"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_sightseeing"
        tools:layout="@layout/fragment_sight_seeing" />
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/navigation_gallery"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_gallery"
        tools:layout="@layout/fragment_gallery" />
    <fragment
        android:id="@+id/navigation_contact_us"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_contact_us"
        tools:layout="@layout/fragment_contact_us" />
</navigation>

為了進行導航,您需要調用NavController的navigation navigate()方法。


使用NavController導航到目的地,該對象在NavHost中管理應用程序導航。 每個NavHost都有自己的對應NavController。

要獲取片段,活動或視圖的NavController,請使用以下方法之一:

NavHostFragment.findNavController(Fragment)
// -- or --
Navigation.findNavController(Activity, @IdRes int viewId)
// -- or --
Navigation.findNavController(View)

檢索NavController后,請使用其navigate()方法導航到目標位置。 navigation()方法接受操作或目標的資源ID。

然后,在導航按鈕的onClick上設置以下內容:

Navigation.findNavController(view).navigate(R.id.viewTransactionsAction);

參考: 官方文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM