🥄
One Library
  • 🤖onelib
  • 🏡Home
    • Overview
    • Setup Project
    • What's New
  • ☀️Fundamental
    • Basic Android
    • Data Flow
    • Dependency Injection
      • Hilt
      • Koin
  • 📿Layers
    • Data Layer
    • Domain Layer
    • Presentation Layer
  • 🪶Features
    • Views
      • Activity
      • Fragment
      • Bottom Sheet
      • Dialog
    • Adapters
      • Recycler Adapter
      • Filterable Adapter
      • Paging Adapter
      • Selectable Adapter
      • ViewPager Adapter
    • Firebase
      • One Firebase Auth
      • One Firebase Firestore
      • One Firebase Realtime
      • One Firebase Storage
    • Extension Functions
      • Any
      • Common
      • Context
      • Edittext
      • Image View
      • LiveData
      • OneMap
      • State Flow (Jetpack Compose)
      • View
    • Notification
    • One State View
    • Local Database
    • Permission
    • Validation
      • Passive Validator
      • Reactive Validator
      • Extension Validation
Powered by GitBook
On this page
  • Single Selectable Adapter
  • Multiple Selectable Adapter
  • Example in Views
  1. Features
  2. Adapters

Selectable Adapter

Last updated 1 year ago

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 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

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.

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 . For example:

🪶
BaseAsyncRecyclerAdapter
lazy