首页 > 解决方案 > 切换片段时出现RecyclerView的副本,重叠(Android,Kotlin)

问题描述

我目前在我的应用程序中有一个 RecyclerView,它使用房间数据库中的文本制作盒子。但是,每当我使用按钮转到我的条目创建片段并返回时,似乎都有相同 recyclerview 的重叠副本。有谁知道我该如何解决这个问题?

在此处输入图像描述

这是与我的后退/片段切换按钮相关的代码:

private fun saveEntry() {
        if (EntryTitle.text.isNullOrEmpty()) {
            Toast.makeText(context, "Your title field is empty", Toast.LENGTH_SHORT).show()
        }
        if (EntrySubtitle.text.isNullOrEmpty()) {
            Toast.makeText(context, "Your subtitle field is empty", Toast.LENGTH_SHORT).show()
        }
        if (EntryText.text.isNullOrEmpty()) {
            Toast.makeText(context, "Your main content field is empty", Toast.LENGTH_SHORT).show()
        }

        launch {
            val journal = Journal()
            journal.title = EntryTitle.text.toString()
            journal.subTitle = EntrySubtitle.text.toString()
            journal.journalText = EntryText.text.toString()
            journal.dateTime = currentDate
            context?.let {
                JournalDatabase.getDatabase(it).JournalDao().insertJournal(journal)
                EntryTitle.setText("")
                EntrySubtitle.setText("")
                EntryText.setText("")
            }
        }
    }

    fun replaceFragment(fragment: Fragment, istransition: Boolean) {
        val fragmentTransition = requireActivity().supportFragmentManager.beginTransaction()

        if (istransition) {
            fragmentTransition.setCustomAnimations(android.R.anim.slide_out_right, android.R.anim.slide_in_left)
        }
        fragmentTransition.replace(R.id.nav_host_fragment, fragment).addToBackStack(fragment.javaClass.simpleName).commit()
    }

这是我的 MainActivity:

package com.google.gradient.red

import android.os.Bundle
import android.view.Menu
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.navigation.NavigationView

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        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.
        appBarConfiguration = AppBarConfiguration(setOf(R.id.nav_home, R.id.nav_settings, R.id.nav_about), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

我已经在网上阅读了一些其他 stackoverflow 问题和资源,这些问题和资源说要设置背景颜色(它掩盖了下面的内容),但这对我来说似乎不是正确的解决方案,并且会导致我的应用程序出现其他问题。

我不确定如何解决我的问题,所以任何帮助将不胜感激!

标签: androidkotlinandroid-fragmentsandroid-recyclerviewfragment

解决方案


我不久前发现了我的问题,但忘记在这里发布我的解决方案,所以我现在就这样做!

问题只是片段重叠。我最终解决了我的问题,仅在我的应用程序中使用导航组件进行导航,而不是使用 2 种不同的方法(正如我帖子中的评论所说)。这解决了我的问题!


推荐阅读