我目前在我的应用程序中有一个RecyclerView,它可以用房间数据库中的文本制作盒子。但是,每当我使用一个按钮转到我的条目创建片段并返回时,似乎都有相同recyclerview的重叠副本。有谁知道我该怎么解决这个问题吗?
x1c 0d1x的数据
这是与返回/片段切换按钮相关的代码:
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()
}
字符串
这是我的主要活动:
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的问题和资源,说要设置一个背景色(这会掩盖下面的内容),但这对我来说似乎不是正确的解决方案,还会导致我的应用程序出现其他问题。
我不知道如何解决我的问题,所以任何帮助将不胜感激!
1条答案
按热度按时间46qrfjad1#
正如博格丹所说,问题在于片段重叠。我最终解决了这个问题,只使用导航组件在我的应用程序中导航,而不是2种不同的方法。