Yet another adapter delegate library.

Overview

Yet another adapter delegate library.

repositories {
    ...
    maven { url 'https://jitpack.io' }
}

...

dependencies {
    implementation("com.github.Miha-x64:Delegapter:-SNAPSHOT")
}

ViewHolder

We use our own ViewHolder class (called just VH) for a bunch of reasons:

  • RecyclerView.ViewHolder is abstract, but it's sometimes necessary to create a “dumb” holder without any special fields or behaviour, thus VH is open
  • There's RecyclerView.ViewHolder.itemView: View, but VH is generic, and has a property VH<V, …>.view: V
  • When using viewBinding, all ViewHolders look the same: they have binding field. VH supports an attachment of any type which is typically ViewBinding: VH<*, B, …>.binding: B
  • Delegapter needs to tie certain ViewHolder type with the corresponding data type for type safety: VH<V : View, B, D>
  • Therefore, VH<*, *, D> has its own bind(D) method which is a common practice (but not forced by the library)

There's a lot of factory functions for creating ViewHolders:

VH(TextView(parent.context).apply {
    layoutParams = RecyclerView.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
    fontRes = R.font.roboto
    textSize = 17f
}, TextView::setText) // VH<TextView, Nothing?, CharSequence>

inflateVH(parent, ItemUserBinding::inflate) { user: User ->
    imageLoader.load(user.photo).into(photoView)
    nameView.text = user.name
} // VH<View, ItemUserBinding, User>

// and more…

Delegate

Delegate is just a ViewHolder factory:

typealias Delegate<D> = (parent: ViewGroup) -> VH<*, *, D>

VH::V and VH::B are actually implementation details of a certain VH, Delegapter does not need them after instantiation, thus <*, *.

A typical Delegate declaration looks like this:

val userDelegate = ::userHolder
private fun userHolder(parent: ViewGroup): Delegate<User> =
    inflateVH(…) { … }

In this example, userDelegate property guarantees object identity (::userHolder expression could give out different instances between invocations). You can just write val userDelegate = { parent -> … }, of course, but method reference, unlike lambda, gives meaningful toString() and helps debugging.

Delegapter

Delegapter is basically a list of (item, delegate) tuples, but their type agreement is guaranteed, like it was a List<<D> Pair<D, Delegate<D>> (non-denotable type in Java/Kotlin).

Delegapter is not an Adapter itself, just a special data structure. Let's use base VHAdapter for convenience:

class SomeAdapter : VHAdapter<VH<*, *, *>>() {

    init { stateRestorationPolicy = … }

    private val d = Delegapter(this /* pass self to get notified */)

    override fun getItemCount(): Int =
        d.size

    override fun getItemViewType(position: Int): Int =
        d.viewTypeAt(position)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH<*, *, *> =
        d.createViewHolder(parent, viewType)

    override fun onBindViewHolder(holder: VH<*, *, *>, position: Int, payloads: List<Any>): Unit =
        d.bindViewHolder(holder, position, payloads)

    fun update(data: Data) {
        d.clear()
        d.add(data.header, headerDelegate)
        d.addAll(data.recommended, recommendationDelegate)
        d.addAll(data.posts, postDelegate)
        // use autocomplete to see all available functions
    }

}

This gives you some flexibility for advanced usage scenarios:

  • Insert items not handled by Delegapter (headers, footers, ads 🤮 ). (Instead of passing this to the constructor, use custom ListUpdateCallback implementation to correct notify*() calls.)
  • Filter out some items without removing them. (This requires a corrected ListUpdateCallback, too.)
  • Use several Delegapters in a single Adapter (IDK why but this should happen at some point).

In order to share RecycledViewPool between several RecyclerViews, you need to preserve the same viewType to Delegate mapping across adapters. This can be achieved using “parent” Delegapter:

val delegapterFather = Delegapter(NullListUpdateCallback)

…

class SomeAdapter : RecyclerView.Adapter<…>() {
    private val d = Delegapter(this, delegapterFather)
    …
}

Apart from skeletal VHAdapter, there are two more: RepeatAdapter and SingleTypeAdapter. They don't use Delegapter but employ VH and Delegate for the ease of use.

ItemDecoration

Decorating different viewTypes is a stressful job. Here's how Delegapter helps you to add spaces between items of certain types:

data.decor(RecyclerView.VERTICAL) {
  // keep 16dp after title, before user
  between({ it === headerDelegate }, { it === userDelegate }, spaceSize = 16f)

  // keep 30dp between any two users
  between({ it === userDelegate }, spaceSize = 30f)
  
  // text units for text items!
  between({ it === textDelegate }, spaceSize = 16f, spaceUnit = COMPLEX_UNIT_SP)
}

Predicates like { it === headerDelegate } look clumsy but are very flexible because you can check for several conditions there, for example, match any type ({ true }) or check for external conditions ({ useTextSpaces && it === textDelegate }).

Any tool can make you happy until it works fine. And make you hate your job when something gets screwed up. A virtue of any abstraction level is an ability to peek into and see what actually happens. If you feel sad, just pass some booleans around: decor(orientation, debugDelegates = true, debugSpaces = true). This will show you which delegate is used for each item (that's where ::function.toString() helps!), or highlight spaces, accordingly.

Screenshot

You might also like...
Add RecyclerView, use Adapter class and ViewHolder to display data.
Add RecyclerView, use Adapter class and ViewHolder to display data.

فكرة المشروع في هذا المشروع سنقوم بعرض قائمة من البيانات للطلاب على واجهة تطبيق Android بإستخدام: مفهوم RecyclerView مفهوم Adapter مفهوم ViewModel محت

Shimo is an adapter for Moshi which randomizes the order of keys when serializing and deserializing

Shimo Shimo is a JsonAdapter.Factory for Moshi which randomizes the order of keys when serializing objects to JSON and when deserializing objects from

An Android Animation library which easily add itemanimator to RecyclerView items.
An Android Animation library which easily add itemanimator to RecyclerView items.

RecyclerView Animators RecyclerView Animators is an Android library that allows developers to easily create RecyclerView with animations. Please feel

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)

Advanced RecyclerView This RecyclerView extension library provides Google's Inbox app like swiping, Play Music app like drag-and-drop sorting and expa

Android library providing simple way to control divider items (ItemDecoration) of RecyclerView
Android library providing simple way to control divider items (ItemDecoration) of RecyclerView

RecyclerView-FlexibleDivider Android library providing simple way to control divider items of RecyclerView Release Note [Release Note] (https://github

Android Library to provide swipe, click and other functionality to RecyclerView

RecyclerViewEnhanced Android Library to provide swipe, click and other functionality to RecyclerView Usage Add this to your build.gradle file dependen

Dividers is a simple Android library to create easy separators for your RecyclerViews
Dividers is a simple Android library to create easy separators for your RecyclerViews

Dividers Dividers is an Android library to easily create separators for your RecyclerViews. It supports a wide range of dividers from simple ones, tha

Android pagination library (updated 01.05.2018)
Android pagination library (updated 01.05.2018)

NoPaginate Android pagination library, based on @MarkoMilos repository Paginate Loading Item Error Item Gradle implementation 'ru.alexbykov:nopaginate

Android library for RecyclerView to manage order of items and multiple view types.
Android library for RecyclerView to manage order of items and multiple view types.

recyclerview-binder Android Library for RecyclerView to manage order of items and multiple view types. Features Insert any items to wherever you want

Releases(0.95)
Owner
Mike
Remote & part-time, Android & server-side, development & consulting.
Mike
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Yoshihito Ikeda 414 Nov 21, 2022
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.

xAdapter: Kotlin DSL 风格的 Adapter 封装 1、简介 该项目是 KotlinDSL 风格的 Adapter 框架封装,用来简化 Adapter 调用,思想是采用工厂和构建者方式获取 Adapter 避免代码中定义大量的 Adapter 类。该项目在 BRVAH 的 Ada

ShouHeng 17 Oct 9, 2022
An adapter to create Android RecyclerViews with sections, providing headers and footers.

⚠ This library is no longer maintained ⚠️ SectionedRecyclerView An adapter to create Android RecyclerViews with sections, providing headers and footer

Tomás Ruiz-López 809 Dec 21, 2022
Don't write a RecyclerView adapter again. Not even a ViewHolder!

LastAdapter Don't write a RecyclerView adapter again. Not even a ViewHolder! Based on Android Data Binding Written in Kotlin No need to write the adap

Miguel Ángel Moreno 781 Dec 19, 2022
A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

##PowerfulRecyclerViewAdapter A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as

null 313 Nov 12, 2022
kotlin dsl for kids to simplify RecyclerView.Adapter logic

KidAdapter RecyclerView adapter for kids. A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter. With KidAdapter y

Eugeniu Tufar 56 Nov 27, 2022
A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps

Infinite Recycler View A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps. This library offers you a custom adapt

IB Sikiru 26 Dec 10, 2019
Reproducible sample with Fix for Memory Leak in RecyclerView Adapter

Memory Leak RecyclerView Adapter Reproducible Sample with Fix Video Instructions: https://www.youtube.com/c/awesomedevnotes Code Only the relevant and

Awesome Dev Notes | Android Dev Notes YouTube 7 Jun 7, 2022
RecyclerView With No Adapter | Available For Jetpack Compose

About This Project Available on Google Dev Library Click Here RecyclerView No Adapter (Adapter Has Been Handled) RecyclerView No Adapter Using ViewBin

Faisal Amir 142 Oct 18, 2022
Easy RecyclerView Adapter

GenericAdapter Easy RecyclerView Adapter Getting started build.gradle allprojects { repositories { // ... maven { url 'https://jit

JaredDoge 4 Dec 3, 2021