[英]Chip Group listener change chip position when fragment recreate
I'm creating a chip programmatically. 我正在以编程方式创建芯片。 The chips are created but when the app stops (not destroyed) and re-opens, the position of the chip changes in the chip Group Listener. 芯片被创建,但是当应用停止(未销毁)并重新打开时,芯片的位置在芯片组侦听器中发生变化。
Here is my code: 这是我的代码:
private lateinit var playlist: Array<Pair<String, String>>
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
for (item in playlist){
createChip(item.first)
}}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
chip_group.setOnCheckedChangeListener { chipGroup, position ->
for (i in 0 until chipGroup.childCount){
val chip = chipGroup.getChildAt(i)
chip.isClickable = chip.id != chipGroup.checkedChipId
}
Toast.makeText(context, position.toString(), Toast.LENGTH_SHORT).show()
}}
fun createChip(name: String){
val chip = Chip(chip_group.context)
chip.text = name
chip.isClickable = true
chip.isCheckable = true
chip.isCheckedIconVisible = false
chip_group.addView(chip)
}
I'm expecting the chip to have the same position when the app re-opens, but instead the position increases. 我希望重新打开应用程序时,芯片的位置相同,但位置会增加。
You are mixing id
with position
. 您正在将id
与position
混合。
onCheckedChanged(ChipGroup group, int checkedId)
is interface method called when the checked chip has changed. onCheckedChanged(ChipGroup group, int checkedId)
是已检查的芯片已更改时调用的接口方法。
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
var index=0
for (item in playlist){
createChip(item.first,index++)
}}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
chip_group.setOnCheckedChangeListener { chipGroup, id ->
for (i in 0 until chipGroup.childCount){
val chip = chipGroup.getChildAt(i)
chip.isClickable = chip.id != chipGroup.checkedChipId
}
Toast.makeText(context, position.toString(), Toast.LENGTH_SHORT).show()
}}
fun createChip(name: String,index:Int){
val chip = Chip(chip_group.context)
chip.text = name
chip.isClickable = true
chip.isCheckable = true
chip.isCheckedIconVisible = false
chip.id=index //maybe you can use tag
chip_group.addView(chip)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.