mirror of
https://github.com/skyline-emu/skyline.git
synced 2024-11-23 13:59:17 +01:00
Add a SelectableGenericAdapter
as subclass of GenericAdapter
`SelectableGenericAdapter` extends `GenericAdapter` with support for marking one item as selected.
This commit is contained in:
parent
e93fdce845
commit
a59f2baa3a
@ -21,7 +21,7 @@ typealias OnFilterPublishedListener = () -> Unit
|
||||
/**
|
||||
* Can handle any view types with [GenericListItem] implemented, [GenericListItem] are differentiated by the return value of [GenericListItem.getViewBindingFactory]
|
||||
*/
|
||||
class GenericAdapter : RecyclerView.Adapter<GenericViewHolder<ViewBinding>>(), Filterable {
|
||||
open class GenericAdapter : RecyclerView.Adapter<GenericViewHolder<ViewBinding>>(), Filterable {
|
||||
companion object {
|
||||
private val DIFFER = object : DiffUtil.ItemCallback<GenericListItem<ViewBinding>>() {
|
||||
override fun areItemsTheSame(oldItem : GenericListItem<ViewBinding>, newItem : GenericListItem<ViewBinding>) = oldItem.areItemsTheSame(newItem)
|
||||
@ -139,3 +139,49 @@ class GenericAdapter : RecyclerView.Adapter<GenericViewHolder<ViewBinding>>(), F
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A [GenericAdapter] that supports marking one item as selected. List items that need to access the selected position should inherit from [SelectableGenericListItem]
|
||||
*/
|
||||
class SelectableGenericAdapter(private val defaultPosition : Int) : GenericAdapter() {
|
||||
var selectedPosition : Int = defaultPosition
|
||||
|
||||
/**
|
||||
* Sets the selected position to [position] and notifies previous and new selected positions
|
||||
*/
|
||||
fun selectAndNotify(position : Int) {
|
||||
val previousSelectedPosition = selectedPosition
|
||||
selectedPosition = position
|
||||
notifyItemChanged(previousSelectedPosition)
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the selected position to [position] and only notifies the previous selected position
|
||||
*/
|
||||
fun selectAndNotifyPrevious(position : Int) {
|
||||
val previousSelectedPosition = selectedPosition
|
||||
selectedPosition = position
|
||||
notifyItemChanged(previousSelectedPosition)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the item at [position] from the list and updates the selected position accordingly
|
||||
*/
|
||||
override fun removeItemAt(position: Int) {
|
||||
super.removeItemAt(position)
|
||||
if (position < selectedPosition)
|
||||
selectedPosition--
|
||||
else if (position == selectedPosition)
|
||||
selectAndNotify(defaultPosition)
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the given item to the list at [position] and updates the selected position accordingly
|
||||
*/
|
||||
override fun addItemAt(position : Int, item : GenericListItem<out ViewBinding>) {
|
||||
super.addItemAt(position, item)
|
||||
if (position <= selectedPosition)
|
||||
selectedPosition++
|
||||
}
|
||||
}
|
||||
|
@ -37,3 +37,7 @@ abstract class GenericListItem<V : ViewBinding> {
|
||||
|
||||
open val fullSpan : Boolean = false
|
||||
}
|
||||
|
||||
abstract class SelectableGenericListItem<V : ViewBinding> : GenericListItem<V>() {
|
||||
val selectableAdapter get() = super.adapter as SelectableGenericAdapter?
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user