mirror of
https://github.com/skyline-emu/skyline.git
synced 2024-06-01 11:58:46 +02:00
8991ccac65
This change lets items get the updated position of their view holder in the adapter. Fixes an issue where the position of items was not updated after being removed from a `SelectableGenericAdapter`.
45 lines
1.8 KiB
Kotlin
45 lines
1.8 KiB
Kotlin
/*
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
* Copyright © 2021 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
*/
|
|
|
|
package emu.skyline.adapter
|
|
|
|
import android.view.ViewGroup
|
|
import com.google.android.material.chip.Chip
|
|
import emu.skyline.R
|
|
import emu.skyline.databinding.HeaderRomFilterBinding
|
|
import emu.skyline.loader.RomFormat
|
|
|
|
object HeaderRomFilterBindingFactory : ViewBindingFactory {
|
|
override fun createBinding(parent : ViewGroup) = HeaderRomFilterBinding.inflate(parent.inflater(), parent, false)
|
|
}
|
|
|
|
typealias OnFilterClickedListener = (format : RomFormat?) -> Unit
|
|
|
|
class HeaderRomFilterItem(private val formats : List<RomFormat>, selectedFormat : RomFormat?, private val onFilterClickedListener : OnFilterClickedListener) : GenericListItem<HeaderRomFilterBinding>() {
|
|
private var selection = selectedFormat?.let { formats.indexOf(it) + 1 } ?: 0
|
|
|
|
override fun getViewBindingFactory() = HeaderRomFilterBindingFactory
|
|
|
|
override fun bind(holder : GenericViewHolder<HeaderRomFilterBinding>, position : Int) {
|
|
val binding = holder.binding
|
|
binding.chipGroup.removeViews(1, binding.chipGroup.childCount - 1)
|
|
for (format in formats) {
|
|
binding.chipGroup.addView(Chip(binding.root.context, null, R.attr.chipChoiceStyle).apply { text = format.name })
|
|
}
|
|
binding.chipGroup.setOnCheckedChangeListener { group, checkedId ->
|
|
for (i in 0 until group.childCount) {
|
|
if (group.getChildAt(i).id == checkedId) {
|
|
selection = i
|
|
onFilterClickedListener(if (i == 0) null else formats[i - 1])
|
|
break
|
|
}
|
|
}
|
|
}
|
|
binding.chipGroup.check(binding.chipGroup.getChildAt(selection).id)
|
|
}
|
|
|
|
override val fullSpan = true
|
|
}
|