Home > Software engineering >  How to select a bottom navigation menu programmatically from a `recyclerView` adapter?
How to select a bottom navigation menu programmatically from a `recyclerView` adapter?

Time:11-24

I have BottomNavigationView in 'activity-home.xml' as below.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@ id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@ id/bottom_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabCradleRoundedCornerRadius="50dp"
            app:fabCradleMargin="10dp">


    <com.google.android.material.bottomnavigation.BottomNavigationView
                    android:id="@ id/nav_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="16dp"
                    android:background="@android:color/transparent"
                    app:menu="@menu/bottom_nav_menu"
                    app:itemIconTint="@color/bottom_nav_color"
                    app:itemTextColor="@color/bottom_nav_color"/>
        
            </com.google.android.material.bottomappbar.BottomAppBar>

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@ id/fab_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/fab_image"
            android:scaleType="center"
            app:maxImageSize="56dp"
            app:tint="@null"
            app:layout_anchor="@id/bottom_appbar"
            android:contentDescription="Show Categories" />
        
        <fragment
            android:id="@ id/nav_host_fragment"
          android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="50dp"
            app:defaultNavHost="true"
            app:layout_anchor="@id/bottom_appbar"
            app:navGraph="@navigation/mobile_navigation" />
        
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Following is the 'mobile_navigation.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/mobile_navigation"
    app:startDestination="@ id/nav_home">

    <fragment
        android:id="@ id/nav_home"
        android:name="com.abc.xyz.ui.fragments.HomeFragment"
        android:label="@string/mobile_navigation_label"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@ id/nav_orders"
        android:name="com.abc.xyz.ui.fragments.OrdersFragment"
        android:label="@string/mobile_navigation_label"
        tools:layout="@layout/fragment_orders"/>

    <fragment
        android:id="@ id/nav_cart"
        android:name="com.abc.xyz.ui.fragments.CartFragment"
        android:label="@string/mobile_navigation_label"
        tools:layout="@layout/fragment_cart"/>

</navigation>

When I click each menu item from the bottom navigation it shows respective fragments and everything works perfectly as expected. Now I wanted to show/open 'CartFragment.kt', ('R.id.nav_cart') when I click a button that is in the recyclerView of the 'fragment_home'. I tried the following way but it didn't work as I expected.

Following is what I have in the onBindViewHolder of the adapter class of the 'fragment_home'

        holder.binding.btnGoToCart.setOnClickListener {
    val activity=context as HomeActivity
activity.supportFragmentManager.beginTransaction().replace(R.id.container,CartFragment()).addToBackStack(null).commit()
    }

Even though I tried as above I think, that's not how I am supposed to do it. All I wanted is to navigate to the bottom menu 'R.id.nav_cart' which should show 'CartFragment'

EDIT:

I also tried the following code in the adapter class of 'fragment_home'. When I hit the button, The bottom menu is highlighted as if it's selected but I don't know what's really happening since the fragment of which is not being shown properly. What I meant is, there is a recyclerView and a few textViews and EditTexts in the fragment but the recyclerView is not being shown, only other views are shown. Also, when I hit other bottom menu items, nothing happens at all except those menu items are highlighted.

            holder.binding.btnGoToCart.setOnClickListener {

                val activity=context as MainActivity

                var fragment:Fragment=CartFragment()
                val bottomNav: BottomNavigationView = context.findViewById(R.id.nav_view)
                bottomNav.setOnNavigationItemSelectedListener { item ->
                    when (item.itemId) {
                        R.id.nav_cart -> {
                            fragment = CartFragment()
                        }
                    }
                    context.supportFragmentManager
                        .beginTransaction()
                        .replace(R.id.container, fragment)
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        .commit()
                    true
                }
                bottomNav.selectedItemId = R.id.nav_cart
            }

CodePudding user response:

I should change my question to "How to select a bottom navigation menu programmatically from the recyclerView adapter?"

Since you're using the navigation library, you would programmatically navigate to the desired destination. See the example in the documentation.

In your case, something like:

holder.binding.btnGoToCart.setOnClickListener { view ->
    view.findNavController().navigate(R.id.nav_cart)
}
  • Related