使用Jetpack导航的BottomNavigation菜单无法导航 [英] BottomNavigation Menu with Jetpack Navigation does not navigate

本文介绍了使用Jetpack导航的BottomNavigation菜单无法导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于BottomNavigationBar,我遇到了一个奇怪的问题,尽管花了很多时间,我还是无法解决.当我以推荐"的方式(从许多教程中)使用它时,它只是无法导航.

I have a strange problem regarding a BottomNavigationBar which I could not solve altough having spent a huge amount of time into it. When I use it in the 'recommended' way (from many tutorials) it just does not navigate.

那么,推荐"方式是什么意思:我具有一个称为"MainActivity"的navHostFragment的单一性.这个主要活动有一个XML布局文件,我在其中放置了BottomNavigationBar.BottomNavigationBar还具有XML布局文件.现在,我有了一个名为"FR_Menu"的片段,其中包含一个Java文件和一个XML布局文件.我也有一个NavGraph.在片段"FR_Menu"的XML布局中,我不使用BottomNavigationBar,而在Java类中,我不实例化BottomNavigationBar,因为我已经在主Activity中完成了此操作.但是使用这种方法,导航不起作用.尽管在底部"FR_Menu"中正确显示了BottomNavigationBar,但单击Bottom却什么也没发生.

So what do I mean by 'recommended' way: I have a single acticity with a navHostFragment called 'MainActivity'. This main activity has a XML layout file in which I put the BottomNavigationBar. The BottomNavigationBar also has a XML layout file. Now I have a Fragment called 'FR_Menu' with a Java file and a XML layout file. I also have a NavGraph. In the XML layout of the Fragment 'FR_Menu' I do not use the BottomNavigationBar and in the Java class I do not instantiate the BottomNavigationBar as I have already done this in the main Activity. But using this approach the Navigation does not work. Altough the BottomNavigationBar is correctly displayd in the Fragment 'FR_Menu', when clicking on the Bottom just nothing happens.

现在来了奇怪的事情.当我使用发布的代码时,未将BottomNavigationBar放在主要活动的XML布局文件中,而是将其分别放在所有片段的每个XML布局文件中(在此示例中为片段FR_Menu),而当我在每个片段的每个Java文件中实例化BottomNavigationBar(在此示例中为片段"FR_Menu"),然后导航就可以完美地工作了.因此,使用这种方法,我必须将BottomNavigationBar放入片段的每个XML布局文件中,并且还必须在Fragments的每个Java文件中实例化BottomNavigationBar.我知道通常不会使用Jetpack Navigation组件,而是应该只在MainActivity中实例化一次,因为BottomNavigationBar应该仅添加到MainActivity的XML布局文件中.

Now here come the strange thing. When I use the code as posted, but not put the BottomNavigationBar in the XML layout file of the main activity and instead put it separately in every XML-layout file of all Fragments (in this example the fragment 'FR_Menu') and when I also instantiate the BottomNavigationBar in every Java file of each Fragment (in this example the fragment 'FR_Menu'), then the Navigation works perfectly. So with this approach I have to put the BottomNavigationBar in every XML layout file of the fragments and also I have to instantiate the BottomNavigationBar in every Java file of the Fragments. I know that normally using the Jetpack Navigation components, this should not be the case and instead I should only have to instantiate once in the MainActivity as the BottomNavigationBar should only be added to the XML layout file of the MainActivity.

有人在尝试实施推荐"方法时有什么想法吗?名称都是正确的(在我显示的示例中可能有一个小错误,因为我不得不对其进行一些简化和调整),因为使用第二种方法时(将BottomNavigationBar放在每个XML布局文件和每个Java文件中)的片段),导航工作完美(我也尝试了多个项目和目的地).

Does anyone have an idea what goes wrong when I am trying to implement the 'recommended' approach? The names are all correct (maybe there is a small error in my shown example because I had to simplify and adjust it a little bit), because when using the second approach (putting the BottomNavigationBar in every XML-layout file and in every Java file of the fragments) the navigation works perfectly (I also tried it with multiple items and destinations).

我确实花了很多时间,我无法弄清楚我的错误.因此,我非常感谢您的每条评论,也非常感谢您的帮助.

I have really spent quite much time on that and I could not figure out what my mistake it. Because of this I would highly appreciate every comment and would be quite thankful for your help.

有人知道这种奇怪行为的原因是什么吗?

Does anyone have an idea what the reason for this strange behaviour might be?

"MainActivity"的Java代码:

Java code of the 'MainActivity':

package com.example.td.bapp;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.td.bapp.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity  {

   
    private  ActivityMainBinding binding;


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


    }

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        binding = ActivityMainBinding.inflate(inflater, container, false);
        NavController navController = Navigation.findNavController(this, R.id.navHostfragment);
        NavigationUI.setupWithNavController(binding.bottomNavigation,navController );
        setContentView(binding.getRoot());
        return binding.getRoot();
    }

}

"MainActivity"的XML布局文件:

XML layout file of the 'MainActivity':

<?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=".MainActivity"
    tools:ignore="ExtraText">


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

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        app:labelVisibilityMode="labeled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorGreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorAccent"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

BottomNavigationBar的XML布局文件:

XML-layout file of BottomNavigationBar:

<?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/FR_LanguageSelection"
        android:icon = "@drawable/ic_add_circle_full"
        android:title = "Language" />





</menu>

片段"FR_Menu"的Java文件

Java file of the Fragment 'FR_Menu'

package com.example.td.bapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.example.td.bapp.databinding.ActivityMainBinding;
import com.example.td.bapp.databinding.FragmentMenuBinding;


public class FR_Menu extends Fragment implements View.OnClickListener {


    // TODO: Rename and change types of parameters



    public FR_Menu() {

    }


    public static FR_Menu newInstance(String param1, String param2) {
        FR_Menu fragment = new FR_Menu();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    private FragmentMenuBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // return inflater.inflate(R.layout.fragment_menu, container, false);
        binding = FragmentMenuBinding.inflate(inflater, container, false);


        /*
       // IMPORTANT REMARK: When I use the following code in the second option this in the second option,
       with the BottomNavigationBar in the XML document, the navigation works well


        NavController navController = Navigation.findNavController(getActivity(), R.id.navHostfragment);
        NavigationUI.setupWithNavController(binding.bottomNavigation,navController );
    */

        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.imageButton_A.setOnClickListener(this);
        binding.imageButton_B.setOnClickListener(this);


    }

    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    @Override
    public void onClick(View view) {



        if(view.getId() == R.id.imageButton_A) {
            String argument = DataBaseEntries.A;

            FR_MenuDirections.ActionFRMenuToFRGenericD action =
                    FR_MenuDirections.actionFRMenuToFRGenericD(argument);
            Navigation.findNavController(view).navigate(action);
        }


        if(view.getId() == R.id.imageButton_B) {
            String argument = DataBaseEntries.B;

            FR_MenuDirections.ActionFRMenuToFRGenericD action =
                    FR_MenuDirections.actionFRMenuToFRGenericD(argument);
            Navigation.findNavController(view).navigate(action);
        }







    }
}

这是片段"FR_Menu"的XML布局文件

Here is the XML layout file of the fragment 'FR_Menu'

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_mainActivity"
        android:layout_width="match_parent"
        android:layout_height="135dp"
        android:background="#435cb53f"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/holo_green_light">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            android:text="Menu" />
    </androidx.appcompat.widget.Toolbar>

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar_mainActivity">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".MainActivity"
            tools:ignore="ExtraText">


            <ImageButton
                android:id="@+id/imageButton_A"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/imageButton_B"
                app:layout_constraintHorizontal_chainStyle="spread"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/menu_A" />

            <ImageButton
                android:id="@+id/imageButton_B"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintStart_toEndOf="@id/imageButton_A"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/menu_B" />






        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>




</androidx.constraintlayout.widget.ConstraintLayout>

NavGraph的XML代码

XML code of the NavGraph

<?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/FR_LanguageSelection">

    <fragment
        android:id="@+id/FR_Menu"
        android:name="com.example.td.bapp.FR_Menu"
        android:label="FR_Menu"
        tools:layout="@layout/fragment_menu" >
        <action
            android:id="@+id/action_FR_Menu_to_FR_LanguageSelection"
            app:destination="@id/FR_LanguageSelection" />
    </fragment>

    <fragment
        android:id="@+id/FR_LanguageSelection"
        android:name="com.example.td.bapp.FR_LanguageSelection"
        android:label="FR_LanguageSelection" >
        <action
            android:id="@+id/action_FR_LanguageSelection_to_FR_Menu"
            app:destination="@id/FR_Menu" />
    </fragment>


</navigation>

推荐答案

onCreateView 是Fragment的方法,不是 Activity 上的方法,因此您的方法在您的框架从未调用 MainActivity .

onCreateView is a method of a Fragment, not a method on Activity, so your method in your MainActivity isn't ever called by the framework.

实际上,根据查看绑定文档.

因此,您的 MainActivity 应该看起来像这样:

Therefore your MainActivity should instead look like:

public class MainActivity extends AppCompatActivity  {
   
    private  ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Inflate, and then call setContentView() on the returned view root
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        
        NavController navController = Navigation.findNavController(this,
            R.id.navHostfragment);
        NavigationUI.setupWithNavController(binding.bottomNavigation, navController);
    }
}

这篇关于使用Jetpack导航的BottomNavigation菜单无法导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆