In Android development with Kotlin, a "Selectable Adapter" typically refers to an implementation of an adapter that allows the user to select one or multiple items from a list displayed by a RecyclerView. This can be useful in scenarios such as selection modes in lists, like email clients where you can select multiple emails to delete or move.
Single Selectable Adapter
SingleSelectableAdapter is a child class of BaseAsyncRecyclerAdapter that has been modified as needed. For example:
classSelectableAdapter(privateval context: Context, selectableItem: (selected: String) -> Unit) : BaseSelectableAdapter<String>(selectableItem = selectableItem) {overridefungetViewBinding(parent: ViewGroup, viewType: Int): ViewBinding {return ItemSelectableBinding.inflate(LayoutInflater.from(context), parent, false) }overridefunbindData(binding: ViewBinding, data: Selectable<String>) {with(binding as ItemSelectableBinding) { tvTitle.text =data.item } }overridefunsetSelected(binding: ViewBinding, selected: Boolean) =with(binding as ItemSelectableBinding) {// Item you want to mark }}
Multiple Selectable Adapter
A multiple selectable adapter is a custom adapter used with components like RecyclerView to enable the selection of multiple items. This type of adapter allows users to select and deselect multiple items from a list. Below is a basic example of how you can implement a multiple selectable adapter in Android using Kotlin.
classSelectableAdapter(privateval context: Context, selectableItem: (selected: String) -> Unit) : BaseSelectableAdapter<String>(isMultipleSelect = true, selectableItem = selectableItem) {overridefungetViewBinding(parent: ViewGroup, viewType: Int): ViewBinding {return ItemSelectableBinding.inflate(LayoutInflater.from(context), parent, false) }overridefunbindData(binding: ViewBinding, data: Selectable<String>) {with(binding as ItemSelectableBinding) { tvTitle.text =data.item } }overridefunsetSelected(binding: ViewBinding, selected: Boolean) =with(binding as ItemSelectableBinding) {// Items you want to mark }}
Example in Views
After the adapter is successfully created, you can call the adapter on the Activity/Fragment to enter data according to your needs, to use it you can use kotlin lazy. For example: