我BottomNavigationView在“activity-home.xml”中有如下內容。
<?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>
以下是“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>
當我單擊底部導航中的每個選單項時,它會顯示相應的片段,并且一切都按預期完美運行。現在,當我單擊recyclerView“fragment_home”中的按鈕時,我想顯示/打開“CartFragment.kt”(“R.id.nav_cart”)。我嘗試了以下方法,但沒有按我預期的那樣作業。
以下是我在onBindViewHolder“fragment_home”的配接器類中的內容
holder.binding.btnGoToCart.setOnClickListener {
val activity=context as HomeActivity
activity.supportFragmentManager.beginTransaction().replace(R.id.container,CartFragment()).addToBackStack(null).commit()
}
即使我像上面那樣嘗試過,我認為這不是我應該做的。我想要的只是導航到底部選單“R.id.nav_cart”,它應該顯示“CartFragment”
編輯:
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
}
uj5u.com熱心網友回復:
我應該將我的問題更改為“如何從 recyclerView 配接器以編程方式選擇底部導航選單?”
由于您使用的是導航庫,因此您可以通過編程方式導航到所需的目的地。請參閱檔案中的示例。
在您的情況下,例如:
holder.binding.btnGoToCart.setOnClickListener { view ->
view.findNavController().navigate(R.id.nav_cart)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/364614.html
