无法在Android中删除碎片
我有两个片段,将它们命名为“红色”和“蓝色”和三个按钮。第一个添加了“红色”片段并将背景更改为红色,第二个添加了“蓝色”片段并将背景更改为蓝色,第三个必须去除当前片段并将颜色更改为上一个片段的颜色。
问题在于,当我单击“删除”按钮时,我会看到烤面包,即删除了片段,但布局不会变为上一个片段的颜色。
var fragment1 = FirstFragment()
var fragment2 = SecondFragment()
lateinit var binding: ActivityMainBinding
var counter: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.redBtn.setOnClickListener {
supportFragmentManager
.beginTransaction()
.add(R.id.placeHolder, fragment1)
.addToBackStack("Red")
.commit()
counter++
binding.count.text = "$counter"
}
binding.blueBtn.setOnClickListener {
supportFragmentManager
.beginTransaction()
.replace(R.id.placeHolder, fragment2)
.addToBackStack("Blue")
.commit()
counter++
binding.count.text = "$counter"
}
binding.removeBtn.setOnClickListener {
val fragmentInstance = supportFragmentManager.findFragmentById(R.id.placeHolder)
if(fragmentInstance is FirstFragment){
supportFragmentManager.beginTransaction()
.remove(FirstFragment())
.commit()
Toast.makeText(this, "Removed Red Fragment", Toast.LENGTH_SHORT).show()
} else {
supportFragmentManager.beginTransaction()
.remove(SecondFragment())
.commit()
Toast.makeText(this, "Removed Blue Fragment", Toast.LENGTH_SHORT).show()
}
counter--
binding.count.text = "$counter"
}
}
}
I have two fragments, named them "Red" and "Blue" and three buttons. The first one adds "Red" fragment and changes the background to red color, the second one adds "Blue" fragment and changes the background to blue color and the third one has to remove the current fragment and change the color to the previous fragment's color.
The problem is that when I click the remove button I see a toast that the fragment is removed but the layout doesn't change to the color of the previous fragment.
var fragment1 = FirstFragment()
var fragment2 = SecondFragment()
lateinit var binding: ActivityMainBinding
var counter: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.redBtn.setOnClickListener {
supportFragmentManager
.beginTransaction()
.add(R.id.placeHolder, fragment1)
.addToBackStack("Red")
.commit()
counter++
binding.count.text = "$counter"
}
binding.blueBtn.setOnClickListener {
supportFragmentManager
.beginTransaction()
.replace(R.id.placeHolder, fragment2)
.addToBackStack("Blue")
.commit()
counter++
binding.count.text = "$counter"
}
binding.removeBtn.setOnClickListener {
val fragmentInstance = supportFragmentManager.findFragmentById(R.id.placeHolder)
if(fragmentInstance is FirstFragment){
supportFragmentManager.beginTransaction()
.remove(FirstFragment())
.commit()
Toast.makeText(this, "Removed Red Fragment", Toast.LENGTH_SHORT).show()
} else {
supportFragmentManager.beginTransaction()
.remove(SecondFragment())
.commit()
Toast.makeText(this, "Removed Blue Fragment", Toast.LENGTH_SHORT).show()
}
counter--
binding.count.text = "$counter"
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不会直接删除,
,然后当您从fragment Manager中添加或删除片段时,请使用相同的片段
对象
fragment1和fragment2
It will not remove directly
Then when you add or remove fragment from fragment manager use the same fragment object
Like you created
fragment1 and fragment2
您好,我认为您只能通过像这样删除Aleady创建的Framgents对象:
您的问题是每次点击删除btn将创建新的片段实例,而不是已经创建的对象
Hello i think you have just to pass the framgents objects that aleady createed to be removed like this:
your problem is every click on remove btn will create new instance of fragment not the object already created