Skip to content

Jintin/ClipAdapter

Repository files navigation

ClipAdapter

CircleCI jitpack

ClipAdapter help you to build scalable Adapter easily. Though we have things like ConcatAdapter today to help us merge different Adapter into one, it still not flexible enough for us to randomly mix different Data/ViewHolder into same Adapter without adding new viewType and also how to create ViewHolder.

With ClipAdapter, you can compose any kind of Data with any ViewHolder together easily.

Install

Add Jitpack repository to your root build.grable:

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

Then add dependency in your module build.gradle:

dependencies {
  implementation 'com.github.jintin:ClipAdapter:1.1.0'
}

Usage

  1. Create you ViewHolder with ClipViewHolder as base class, ie:
private class MyObjectHolder(itemView: View) : ClipViewHolder<MyObject>(itemView) {

    private val title = itemView.findViewById<TextView>(R.id.title)

    override fun onBind(value: MyObject) {
        title.text = value.toString()
    }

    companion object {
        fun provider(): ViewHolderProvider<MyObject> = {
            MyObjectHolder(
                LayoutInflater.from(it.context)
                    .inflate(R.layout.adapter_my_object, it, false)
            )
        }
    }
}
  1. Wrap your data with ClipViewData interface and also link to creation of corresponding ViewHolder, ie:
class MyObjectViewData(
    override val value: MyObject,
    override val holderProvider: ViewHolderProvider<MyObject> = MyObjectHolder.provider()
) : ClipViewData<MyObject>

It's also possible to use ViewBinding, just change your ViewHolder constructor from View to the correct ViewBinding and generate the ViewHolder via the same ViewBinding, ie:

private class MyObjectHolder(
    private val binding: AdapterMyObjectBinding
) : ClipViewHolder<MyObject>(binding.root) {

    override fun onBind(value: MyObject) {
        //...
    }

    companion object {
        fun provider(): ViewHolderProvider<MyObject> = {
            MyObjectHolder(
                AdapterMyObjectBinding.inflate(
                    LayoutInflater.from(it.context),
                    it,
                    false
                )
            )
        }
    }
}
  1. Use ClipAdapter for RecyclerView and pass the data via submitList as ListAdapter does.
val adapter = ClipAdapter()
val data = listOf<ClipViewData<*>>(...)
adapter.submitList(data)

You can go to ./app module for more information.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Jintin/ClipAdapter.

License

The package is available as open source under the terms of the MIT License.

Buy Me A Coffee

About

Scalable Adapter which can easily bind to any kind of Data and ViewHolder.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages