Coder Social home page Coder Social logo

vshnv / adapt Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 773 KB

Adapt is a Kotlin DSL for writing efficient RecyclerView adapters with better usability.

License: MIT License

Kotlin 100.00%
adapter android android-library dsl kotlin kotlin-android recyclerview

adapt's Introduction

Adapt

What is Adapt?

Adapt is an Android Library written in Kotlin with an aim to provide a clean, well-integrated and easy-to-use experience in writing RecyclerView adapters.

It leverages Kotlin to create a typesafe DSL that makes writing Adapters simple.

Documentation: https://vshnv.github.io/adapt/#adapt

Getting Started

Gradle:
dependencies {
    implementation("io.github.vshnv:adapt:0.0.6")
}

Make sure you have Maven Central added as a repository. In your settings.gradle file, verify that mavenCentral() is added as a repository

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral() // <---- Make sure this is present
    }
}

Sample Usage

Assume we have some data class Person to be shown in a RecyclerView.

data class Person(
    val name: String,
    val age: Int,
    val avatarUrl: String
)

Also assume we create a layout file layout_person.xml .

Now we can create a simple adapter for this using adapt:

fun createPersonAdapter() = adapt<Person> {
    create {
        // Inflating view for element
        val personBinding = LayoutPersonBinding.inflate(LayoutInflater.from(it.context), it, false)
        ViewSource.BindingViewSource(personBinding, ViewBinding::getRoot)
    }.bind {
        binding.name.text = data.name
        binding.age.text = "Aged ${data.age}"
    }.withLifecycle { lifecycleOwner ->
        lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
            val avatar = fetchAvatar(avatarUrl) // Some long running task required for this specific binding
            binding.avatar.setImageBitmap(avatar)
        }
    }
}

While the above example uses ViewBinding, you can use DataBinding or just views for the same.

You can now submit values to the adapter created using your function.

val myData = listOf(
    Person("Person A", 31),
    Person("Person B", 25),
    Person("Person C", 52),
)

val adapter = createPersonAdapter()
recyclerView.adapter = adapter
adapter.submitData(myData)

And thats it!

submitData handles async diffing and publishes only changes to your data. For better performance it is recommended to include itemEquals and contentEquals in your adapter config. This is further explained a few sections below.

adapt's People

Contributors

vshnv avatar

Stargazers

Lished avatar Mr. Browner avatar Paul T avatar  avatar emi avatar Sava Dimitrijevic avatar

Watchers

 avatar

Forkers

mrbrowner

adapt's Issues

Not an issue, just wanna ask about adapter

Hey, Thanks you for the library, it's easy to use.

but, how can I add custom functions in adapter?
Can I also add Filterable?

If, YES then how can i add to it.
Thanks in advance.

Type mismatch on ViewSource

Screenshot 2024-07-19 125011

i don't know why this is coming.
Any help would be greatly appreciated.

note: No error & running successfully in older version of Android Studio (hedgehog).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.