【问题标题】:Bottom navigation doesn't show fragments in android jet pack navigation graph底部导航不显示 android jet pack 导航图中的片段
【发布时间】:2020-01-08 11:38:39
【问题描述】:

我有这个应用程序有两个活动,一个是登录、注册和...片段的主机,如果用户注册,它会转到另一个活动,其中包含底部导航和 5 个片段进行切换。问题是第一个活动工作正常,但是当我午餐第二个活动时,底部导航显示并且工作但片段的内容没有显示! 这是我的导航图:

<?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_nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.prototype.nyx.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_walletFragment"
            app:destination="@id/walletFragment" />
    </fragment>
    <fragment
        android:id="@+id/walletFragment"
        android:name="com.prototype.nyx.WalletFragment"
        android:label="fragment_wallet"
        tools:layout="@layout/fragment_wallet" >
        <action
            android:id="@+id/action_walletFragment_to_addProductFragment"
            app:destination="@id/addProductFragment" />
    </fragment>
    <fragment
        android:id="@+id/addProductFragment"
        android:name="com.prototype.nyx.AddProductFragment"
        android:label="fragment_add_product"
        tools:layout="@layout/fragment_add_product" >
        <action
            android:id="@+id/action_addProductFragment_to_settingsFragment"
            app:destination="@id/settingsFragment" />
    </fragment>
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.prototype.nyx.SettingsFragment"
        android:label="fragment_settings"
        tools:layout="@layout/fragment_settings" >
        <action
            android:id="@+id/action_settingsFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.prototype.nyx.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
</navigation>

这是我的主要活动及其布局:

package com.prototype.nyx;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpNavigation();
    }

    public void setUpNavigation() {
        bottomNavigationView = findViewById(R.id.bottom_nav);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.main_nav_host_fragment);
        System.out.println("number of fragments is:"+navHostFragment.getChildFragmentManager()
                .getFragments().size());
        NavigationUI.setupWithNavController(bottomNavigationView,
                navHostFragment.getNavController());
    }
}

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/main_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/main_nav_graph" />



    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

还有菜单:

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

    <item
        android:id="@id/homeFragment"
        android:icon="@drawable/ic_home"
        app:showAsAction="always"
        android:title="">
    </item>


    <item
        android:id="@id/walletFragment"
        android:icon="@drawable/ic_wallet"
        app:showAsAction="always"
        android:title="">
    </item>

    <item
        android:id="@id/addProductFragment"
        android:icon="@drawable/ic_add"
        app:showAsAction="always"
        android:title="">
    </item>

    <item
        android:id="@+id/settingsFragment"
        android:icon="@drawable/ic_settings"
        app:showAsAction="always"
        android:title="">
    </item>

    <item
        android:id="@id/profileFragment"
        android:icon="@drawable/ic_person_black_24dp"
        app:showAsAction="always"
        android:title="">
    </item>
</menu>

一开始我以为是主要的activity布局,所以我也尝试了frame layout和coordinator layout。 但没有任何效果。 我的片段之一:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="256dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.063"
        tools:src="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>

有什么问题?

【问题讨论】:

    标签: android android-fragments navigation bottomnavigationview


    【解决方案1】:

    尝试在navgraph中查看fragment的包名和fragment名attr

    【讨论】:

    • 它们在所有片段和nav_graph中都是相同的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2020-03-23
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多