Selectable Adapter

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:

class SelectableAdapter(
    private val context: Context,
    selectableItem: (selected: String) -> Unit
) : BaseSelectableAdapter<String>(selectableItem = selectableItem) {

    override fun getViewBinding(parent: ViewGroup, viewType: Int): ViewBinding {
        return ItemSelectableBinding.inflate(LayoutInflater.from(context), parent, false)
    }

    override fun bindData(binding: ViewBinding, data: Selectable<String>) {
        with(binding as ItemSelectableBinding) {
            tvTitle.text = data.item
        }
    }

    override fun setSelected(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.

class SelectableAdapter(
    private val context: Context,
    selectableItem: (selected: String) -> Unit
) : BaseSelectableAdapter<String>(isMultipleSelect = true, selectableItem = selectableItem) {

    override fun getViewBinding(parent: ViewGroup, viewType: Int): ViewBinding {
        return ItemSelectableBinding.inflate(LayoutInflater.from(context), parent, false)
    }

    override fun bindData(binding: ViewBinding, data: Selectable<String>) {
        with(binding as ItemSelectableBinding) {
            tvTitle.text = data.item
        }
    }

    override fun setSelected(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:

class SelectableAdapterActivity : BaseActivity<ActivitySelectableAdapterBinding>() {

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, SelectableAdapterActivity::class.java))
        }
    }

    private val adapter by lazy {
        SelectableAdapter(this) { item ->
            // Handle selected item
        }
    }

    override fun getViewBinding(): ActivitySelectableAdapterBinding {
        return ActivitySelectableAdapterBinding.inflate(layoutInflater)
    }

    override fun initIntent() {}

    override fun initUI() {
        binding.rvSelected.adapter = adapter
    }

    override fun initAction() {}

    override fun initProcess() {
        val data = mutableListOf<String>()
        for (i in 0..40) {
            data.add("Item $i")
        }

        adapter.setData = data.toSelectable()
    }
}

.toSelectable() is a part of onelib extension functions.

Last updated