gpt4 book ai didi

android - fragment 彼此堆叠

转载 作者:行者123 更新时间:2023-12-02 13:43:52 25 4
gpt4 key购买 nike

在Android Studio中,我使用“BottomNavigationView”启动了一个新项目,现在当我在模拟器中运行该应用程序时,默认情况下会存在Home Fragment,但是当我切换到另一个片段时就永远不会替换它。而是,新片段彼此替换,同时仍位于第一个片段的顶部。因此,如果我有3个片段,则片段1停留在 Activity 中,而片段1(重复),片段2和片段3都在片段1(原始)的顶部相互替换。我不知道如何摆脱原始片段,甚至从何而来。我是Android开发和学习Kotlin的新手,所以我很难在Kotlin中找到建议。这是我的MainActivity中的代码:

package com.example.android.pointmax


import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.android.pointmax.ui.home.HomeFragment
import com.example.android.pointmax.ui.recommended.RecommendedFragment
import com.example.android.pointmax.ui.wallet.WalletFragment
import timber.log.Timber


class MainActivity : AppCompatActivity() {

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home-> {
val fragment = HomeFragment.newInstance()
replaceFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_wallet -> {
replaceFragment(WalletFragment())
return@OnNavigationItemSelectedListener true
}
R.id.navigation_recommended -> {
replaceFragment(RecommendedFragment())
return@OnNavigationItemSelectedListener true
}
}
false
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Plant tree to enable Debugging with Timber
Timber.plant(Timber.DebugTree())

// Find the bottomNavigation bar
val navView: BottomNavigationView = findViewById(R.id.nav_view)

// Find the fragment that will host the different fragments
val navController = findNavController(R.id.nav_host_fragment)

// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_wallet, R.id.navigation_recommended))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)

// Set click listeners to respond to bottom navigation selections
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

}

private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment,fragment)
.commit()
}
}

MainActivity.xml是默认情况下的创建方式,带有ConstraintLayout,在其下面嵌套有bottomNavigationView和nav_host_fragment。我非常感谢您的帮助。先感谢您。

最佳答案

之所以会出现这种奇怪的导航行为,是因为您同时尝试了两种导航方法

1.导航组件

2.片段管理器

在使用Android Jetpack的Navigation组件时,您应该创建Navigation graph并将其用于片段之间的导航。

该链接应帮助您了解有关导航图的更多信息。

https://developer.android.com/guide/navigation/navigation-design-graph

关于android - fragment 彼此堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com