Home > database >  BottomNavigationView does not respond to clicks
BottomNavigationView does not respond to clicks

Time:11-10

I can't bind Bottom Navigation View to FragmentContainerView. Clicks on the Bottom Navigation View have no effect.

Code: fragment_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    tools:context=".screen.fragment.TabsFragment"
    xmlns:tools="http://schemas.android.com/tools"
    >

    <androidx.fragment.app.FragmentContainerView
        android:id="@ id/containerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"

        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/graph_navigation_tabs"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@ id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/menu_tabs"

        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        android:background="@color/blue_dark"
        />
</LinearLayout>

TabsFragment.kt

class TabsFragment : Fragment(R.layout.fragment_tabs)
{
    private lateinit var binding : FragmentTabsBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentTabsBinding.inflate(layoutInflater, container, false)
        val navHost = childFragmentManager.findFragmentById(R.id.containerView) as NavHostFragment
        val navView: BottomNavigationView = binding.bottomNavigationView
        val navController = navHost.navController

        NavigationUI.setupWithNavController(navView, navController)

        return binding.root
    }
}

graph_navigation_tabs.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"
    android:id="@ id/graph_navigation_tabs"
    app:startDestination="@id/graph_shop">

    <include app:graph="@navigation/graph_shop" />
    <include app:graph="@navigation/graph_favorite" />
    <include app:graph="@navigation/graph_shopping_card" />
    <include app:graph="@navigation/graph_profile" />
</navigation>

menu_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@ id/Explorer"
        android:title="Explorer"
        android:icon="@drawable/ic_explorer_dot"
        />
    <item
        android:id="@ id/buy"
        android:title="Shop"
        android:icon="@drawable/ic_buy"
        />

    <item
        android:id="@ id/favorite"
        android:title="Favorite"
        android:icon="@drawable/ic_favorite"
        />

    <item
        android:id="@ id/profile"
        android:title="Profile"
        android:icon="@drawable/ic_profile"
        />
</menu>

I tried using newview.setupWithNavController(NavController), but it didn't help I tried to replace nested graphs with ordinary fragments, but the effect is the same

CodePudding user response:

Your bottomNavigation doesn't toggle fragments because your menu items id must be the same as the included navgraph name.

Example: In your nav_graph.xml

<include app:graph="@navigation/graph_shop" />

In your menu.xml

<item
    android:id="@ id/graph_shop"
     ...
    />
  • Related