Giter VIP home page Giter VIP logo

anytypeadapter's Introduction

AnyTypeAdapter

The RecyclerView Adapter that is able to compose different view types in one list

List with pagination Dynamic content Static content Static content - clicks

This adapter is:

  • Type-safe and multitype
  • Written once and used for each list in a project (including DiffUtil)
  • Without the need to create additional interfaces or models-wrappers. Only those ones you really need

Under the hood:

  • Kotlin (with using coroutines)
  • Android View Binding

Dependencies:

In your module level build.gradle add:

android {
    ...
    buildFeatures {
        viewBinding true
    }
    ...
}

dependencies {
    ...
    implementation 'io.github.vsukharew:anytypeadapter:x.y.z'
    ...
}

Usage:

  1. InheritAnyTypeDelegate and AnyTypeViewHolder
class TrackDelegate : AnyTypeDelegate<Track, DelegateTrackBinding, Holder>() {

    override fun createViewHolder(itemView: View): Holder = Holder(
        // generated view binding file
        DelegateTrackBinding.bind(itemView)
    )

    override fun getItemViewType(): Int = R.layout.delegate_track

    override fun getItemId(item: Track): String = item.id

    class Holder(
        binding: DelegateTrackBinding
    ) : AnyTypeViewHolder<Track, DelegateTrackBinding>(binding) {
        // views declaration

        override fun bind(item: Track) {
            // bind data
        }
    }
}
class PerformerDelegate : AnyTypeDelegate<Performer, DelegatePerformerBinding, Holder>() {

    override fun createViewHolder(itemView: View): Holder = Holder(
        // generated view binding file
        DelegatePerformerBinding.bind(itemView)
    )

    override fun getItemViewType(): Int = R.layout.delegate_performer

    override fun getItemId(item: Performer): String = item.id

    class Holder(
        binding: DelegatePerformerBinding
    ) : AnyTypeViewHolder<Performer, DelegatePerformerBinding>(binding) {
        // views declaration

        override fun bind(item: Performer) {
            // bind data
        }
    }
}

In case of not having data to be bound inherit NoDataDelegate and NoDataViewHolder

class GoPremiumDelegate(
    private val subscribeToPremiumListener: () -> Unit
) : NoDataDelegate<GoPremiumBinding>() {

    override fun createViewHolder(itemView: View): NoDataViewHolder<GoPremiumDelegate> =
        Holder(GoPremiumBinding.bind(itemView))

    override fun getItemViewType(): Int = R.layout.delegate_go_premium

    inner class Holder(binding: GoPremiumDelegate) :
        NoDataViewHolder<GoPremiumDelegate>(binding) {
        init {
            binding.retryBtn.setOnClickListener {
                retryClickListener.invoke()
            }
        }
    }
}
  1. Create adapter and delegates instances
//class-scope variables
val trackDelegate = TrackDelegate()
val performerDelegate = PerformerDelegate()
val goPremiumDelegate = GoPremiumDelegate() { /* subscribe to premium implementation */ }
val adapter = AnyTypeAdapter()
  1. Create AnyTypeCollection, fill it with the data and pass it to adapter
// combine builder methods depending on your data
AnyTypeCollection.Builder()
    .add(tracks, trackDelegate)
    .addIf(goPremiumDelegate) { user !is PremiumUser }
    .addIfNotEmpty(performers, performerDelegate)
    .build()
    .let { anyTypeAdapter.setCollection(it) }

anytypeadapter's People

Contributors

vsukharew avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.