繁体   English   中英

碎片彼此堆叠

[英]Fragments are stacking on top of each other

在 Android Studio 中,我使用“BottomNavigationView”启动了一个新项目,现在当我在模拟器中运行应用程序时,默认情况下 Home Fragment 在那里,但当我切换到另一个片段时永远不会被替换。 相反,新片段相互替换,同时仍位于第一个片段之上。 因此,如果我有 3 个片段,片段 1 会停留在活动中,而片段 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 组件时,您应该创建导航图并使用它们在片段之间导航。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM