Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
An opinionated library for working with RecyclerView adapters
License
italankin/adapter-delegates
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
An opinionated library for working with RecyclerView ‘s adapters.
dependencyResolutionManagement < repositories < // . maven < url 'https://jitpack.io' > > >
Add dependency in your module (e.g. app/build.gradle ):
dependencies < implementation 'me.italankin:adapter-delegates:1.0.0' >
Base components of the library are AdapterDelegate and CompositeAdapter .
You can either implement AdapterDelegate directly or use BaseAdapterDelegate as a base class for your delegates:
class TitleAdapterDelegate : BaseAdapterDelegateTitleAdapterDelegate.ViewHolder, Title>() < override val layoutRes: Int = R.layout.item_title override fun isType(position: Int, item: Any): Boolean < return item is Title > override fun createViewHolder(itemView: View): ViewHolder < return ViewHolder(itemView) > override fun onBind(holder: ViewHolder, position: Int, item: Title) < holder.title.text = item.title > class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) < val title: TextView = itemView.findViewById(R.id.title) > >
Then create an instance of CompositeAdapter and set as adapter of RecyclerView :
val adapter = CompositeAdapter.BuilderAny>(this) .add(TitleAdapterDelegate()) // add more adapters if you need // an optional viewType can be provided: .add(R.layout.item_title, TitleAdapterDelegate()) .create() // provide a dataset for adapter adapter.setDataset(createDataset()) // finally, set RecyclerView adapter recyclerView.adapter = adapter
You can use CompositeAdapter.Builder methods to set dataset and attach adapter to RecyclerView :
val adapter = CompositeAdapter.BuilderAny>(this) .add(TitleAdapterDelegate()) .dataset(createDataset()) .recyclerView(recyclerView) .create()
The library provides Diffable interface:
class Title( // provide unique ID for this item override val id: Long, val title: String ) : Diffable < override fun contentEquals(other: Diffable): Boolean < return other is Title && title == other.title > >
val adapter = DifferCompositeAdapter.BuilderDiffable>(this) .add(TitleAdapterDelegate()) .recyclerView(recyclerView) .setHasStableIds(true) .create() // submit list to apply changes adapter.submitDataset(createDataset()) // for any change in the dataset create a copy of it // it is required by DiffUtil to work correctly val newDataset = ArrayList(adapter.dataset) newDataset.removeFirst() adapter.submitDataset(newDataset)
MIT License Copyright (c) 2022 Igor Talankin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
About
An opinionated library for working with RecyclerView adapters