切换片段时出现 RecyclerView 的副本,重叠(Android,Kotlin)
Copies of RecyclerView appearing when switching fragments, overlapping (Android, Kotlin)
我目前在我的应用程序中有一个 RecyclerView,它使用房间数据库中的文本创建框。但是,每当我使用按钮转到我的条目创建片段并返回时,似乎有相同 recyclerview 的重叠副本。有谁知道我该如何解决这个问题?
这是与我的 back/fragment 开关按钮相关的代码:
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()
}
}
我已经在网上阅读了一些其他的 Whosebug 问题和资源,这些问题和资源说要设置背景颜色(覆盖下面的内容),但这对我来说似乎不是正确的解决方案,并且会导致我的应用程序出现其他问题。
我不确定如何解决我的问题,因此非常感谢您的帮助!
我刚才解决了我的问题,但忘了 post 我的解决方案,所以我现在就这样做!
问题只是片段重叠。我最终通过在我的应用程序中仅使用导航组件而不是 2 种不同的方法来解决我的问题(如我的评论 post 所说)。这解决了我的问题!
我目前在我的应用程序中有一个 RecyclerView,它使用房间数据库中的文本创建框。但是,每当我使用按钮转到我的条目创建片段并返回时,似乎有相同 recyclerview 的重叠副本。有谁知道我该如何解决这个问题?
这是与我的 back/fragment 开关按钮相关的代码:
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()
}
}
我已经在网上阅读了一些其他的 Whosebug 问题和资源,这些问题和资源说要设置背景颜色(覆盖下面的内容),但这对我来说似乎不是正确的解决方案,并且会导致我的应用程序出现其他问题。
我不确定如何解决我的问题,因此非常感谢您的帮助!
我刚才解决了我的问题,但忘了 post 我的解决方案,所以我现在就这样做!
问题只是片段重叠。我最终通过在我的应用程序中仅使用导航组件而不是 2 种不同的方法来解决我的问题(如我的评论 post 所说)。这解决了我的问题!