kotlin dsl for kids to simplify RecyclerView.Adapter logic

Overview

logo

KidAdapter

RecyclerView adapter for kids.

A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter.

With KidAdapter you can use all power of kotlin dsl to avoid wired standard implementation of RecyclerView.Adapter view types. You can easily maintain, update, move, swap, remove or add new view types using dsl code blocks.

Bonus: Almost all logic by default works with DiffUtil

Maven Central Build Status ktlint Android Arsenal

Gradle

Gradle:

// only for androidx projects
implementation 'com.link184:kid-adapter:1.3.7'

// for projects with android.support.*
implementation 'com.link184:kid-adapter:1.2.2'

Samples

Simple adapter without view types:

val adapter = recyclerView.setUp<MyObject> { //an extension on RecyclerView which return a instance of adapter
    // optional, set layout manager or leave to use default linear vertical
    withLayoutManager(GridLayoutManager(context, 3)) 
    // set layout res id for each adapter item
    withLayoutResId(R.layout.item_text) 
    // set adapter items
    withItems(mutableListOf(MyObject(name = "one"), MyObject("two"), MyObject("three")))
    // set bind action
    bind { item -> // this - adapter view holder itemView, item - current item
        this.setBackgroundColor(getRandomColor())
        stringName.text = item.name //string view is a synthetic inflated view from bind function context 
        setOnClickListener { ... } //click listener on ViewHolder.itemView
    }
    // if you need adapter position use this method instead of "bind"
    bindIndexed { item, index -> // item - current item, index - adapterPosition
        ...
    }
}

//runtime adapter update
adapter + MyObject("four")
adapter += mutableListOf(MyObject("1"), MyObject("2"))
adapter[2] = MyObject("two")
adapter - MyObject("four")
adapter + MyObject("five") + MyObject("six") - MyObject("one")

Adapter with view types:

val adapter = recyclerView.setUp {
    // declare a viewtype
    withViewType("FIRST_STRING_TAG") { // tag is optional but is useful for future updates when you have multiple view typs with the same item types
        // optional, set layout manager or leave to use default linear vertical
        withLayoutManager { GridLayoutManager(context, 3) }
        // set layout res id to current view type
        withLayoutResId(R.layout.item_text)
        // set items to currect view type
        withItems(mutableListOf("one", "two", "three", "four", "five", "six", "seven"))
        // optional, a callback from DiffUtils, by default it compare items with equals() method, set it if you need a custom behavior
        withContentComparator<String> { oldItem, newItem ->
            oldItem.length > newItem.length
        }
        // optional, a callback from DiffUtils, by default it compare items with equals() method, set it if you need a custom behavior
        withItemsComparator<Int> { oldItem, newItem -> 
            oldItem.hashCode() == newItem.hashCode()
        }
        // set bind action
        bind<String> { // this - is adapter view hoder itemView, item - current item
            stringName.text = it
            setOnClickListener { ... } //click listener on ViewHolder.itemView
        }
        // if you need adapter position use this method instead of "bind"
        bindIndexed { item, index -> // item - current item, index - adapterPosition
            ...
        }
    }

    withViewType {
        withLayoutResId(R.layout.item_int)
        withItems(mutableListOf(1, 2, 3, 4, 5, 6))
        bind<Int> {
            intName.text = it.toString()
        }
    }


    withViewType("SECOND_STRING_TAG") {
        withLayoutResId(R.layout.item_text)
        withItems(mutableListOf("eight", "nine", "ten", "eleven", "twelve"))
        bind<String> {
            stringName.text = it
        }
    }
    
    //Update adapter as needed
    adapter update { ... }
}

Update multiple view type adapter.

adapter update {
    insertBottom(mutableListOf("thirteen", "fourteen"), SECOND_STRING_TAG)
    insertTop(mutableListOf("asd", "asd")) // there are no tag, library automatically detect and insert items on first list of strings
    insert(2, mutableListOf(4, 5, 6, 7)) // no tag, items will be inserted in first list of integers
    removeItems(mutableListOf(1,3,6))
    removeItems(mutableListOf("one", "thirteen"))
    removeAll()
}

adapter restructure {
    insert(2, "tag") {
        withItems(items)
        withLayoutResId(android.R.layout.list_content)
        bind<Int> {
            println("We are here $it")
        }
    }
    insertTop("top1Tag") { ... }
    insert(3, "insertAt3Tag") { ... }
    insertBottom("bottom1Tag") { ... }
    replace("top1Tag") { ... }
    swap(0, 3)
    removeAll()
    insertTop("top1Tag") { ... }
}

Proguard

Don't worry about that.

License

See the LICENSE file for details.

You might also like...
A flexible view for providing a limited rect window into a large data set,just like a two-dimensional RecyclerView.  It different from RecyclerView is that it's two-dimensional(just like a Panel) and it pin the itemView of first row and first column in their original location. ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

RecyclerView : SleepQualityTracker with RecyclerView app
RecyclerView : SleepQualityTracker with RecyclerView app

RecyclerView - SleepQualityTracker with RecyclerView app SleepQualityTracker with RecyclerView This app builds on the SleepQualityTracker developed pr

TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.
TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.

TikTok-RecyclerView Demo About This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoP

Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView
Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView Simple and easy way to Paginating a RecyclerView Android

KotlinDSL - Gradle dependency management using buildSrc and Kotlin DSL
KotlinDSL - Gradle dependency management using buildSrc and Kotlin DSL

Gerenciamento de dependências Gradle usando buildSrc e Kotlin DSL João Santos Qu

An adapter to create Android RecyclerViews with sections, providing headers and footers.
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

Comments
  • Data Binding support

    Data Binding support

    Describe the bug It seems that data binding isn't supported

    Code

        <layout 
               xmlns:android="http://schemas.android.com/apk/res/android"
               xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
            <variable
                name="product"
                type="com.zarifa.alaa.data.model.product.Product" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
     
        <TextView
                android:text="@{product.name}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_small"
                app:layout_constraintStart_toEndOf="@+id/ProductImageView"
                app:layout_constraintTop_toTopOf="parent" />
    
        //... other views
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        </layout>
    
    val adapter = binding.recyclerItems.setUp<Product> {
                    withLayoutManager(LinearLayoutManager(requireActivity()))
                    withLayoutResId(R.layout.item_product)
                    withItems(products MutableList<Product>)
                    bindIndexed { item, index ->
                       
                     // I can't access the variable name "product"
                       product = item
    
                    }
                }
    
    help wanted 
    opened by alaa-abuzarifa 6
Owner
Eugeniu Tufar
Eugeniu Tufar
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
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
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
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
Add RecyclerView, use Adapter class and ViewHolder to display data.

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

Shaima Alghamdi 3 Nov 18, 2021
Just another one easy-to-use adapter for RecyclerView :rocket:

Elementary RecyclerView Adapter Another one easy-to-use adapter for RecyclerView ?? Features: DSL-like methods for building adapters similar to Jetpac

Roman Andrushchenko 29 Jan 6, 2023
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022