https://github.com/redmadrobot/itemsadapter
The simple adapter to render static data in RecyclerView
https://github.com/redmadrobot/itemsadapter
adapter android library recyclerview recyclerview-adapter
Last synced: 9 days ago
JSON representation
The simple adapter to render static data in RecyclerView
- Host: GitHub
- URL: https://github.com/redmadrobot/itemsadapter
- Owner: RedMadRobot
- License: mit
- Created: 2020-11-15T15:24:20.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-07-18T09:44:16.000Z (almost 4 years ago)
- Last Synced: 2025-04-13T17:04:40.565Z (9 days ago)
- Topics: adapter, android, library, recyclerview, recyclerview-adapter
- Language: Kotlin
- Homepage:
- Size: 127 KB
- Stars: 12
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ItemsAdapter
[][mavenCentral] [][ci] [][license]The simple adapter to render static data in `RecyclerView`.
---
- [Installation](#installation)
- [Rationale](#rationale)
- [When should I not use `ItemsAdapter`?](#when-should-i-not-use-itemsadapter)
- [Usage](#usage)
- [`ViewBinding` support](#viewbinding-support)
- [Features](#features)
- [Context](#context)
- [Looping mode](#looping-mode)
- [Contributing](#contributing)
- [License](#license)## Installation
Add the dependency:
```groovy
repositories {
mavenCentral()
google()
}dependencies {
implementation("com.redmadrobot.itemsadapter:itemsadapter:1.1")
// or if you use viewbinding
implementation("com.redmadrobot.itemsadapter:itemsadapter-viewbinding:1.1")
}
```## Rationale
There are cases when you just need to render a simple list of static data without the hassle.
You don't want to use complex solutions like [Epoxy] or [Groupie] for this purpose.
This task is `ItemsAdapter` was created for.
`ItemsAdapter` offers you simple DSL to create adapters.#### When should I not use `ItemsAdapter`?
- **When** you need to update and re-draw data in adapter
- **When** you need to render elements with complex logic## Usage
The simplest case of usage is:
```kotlin
recyclerView.adapter = itemsAdapter(regions) { region ->
bind(R.layout.view_region) { // this: View
view_region_title.text = region.title
view_region_description.text = region.description
}
}
```If you have more than one view type, you can use operator `when`:
```kotlin
recyclerView.adapter = itemsAdapter(contactsItems) { item ->
when (item) {
is ContactsItem.Header -> bind(R.layout.view_contacts_header) {
view_contacts_header_letter.text = item.letter
}is ContactsItem.Entry -> bind(R.layout.view_contacts_entry) {
view_contacts_entry_name.text = item.name
view_contacts_entry_phone.text = item.phone
}
}
}
```> :exclamation: Note that kotlin synthetics are deprecated since Kotlin 1.4.20, so it is better to use ViewBinding or `findViewById`.
### `ViewBinding` support
If you use `ViewBinding`, use `itemsadapter-viewbinding` in place of `itemsadapter`.
Then you can use method `bind` with `ViewBinding`:
```kotlin
recyclerView.adapter = itemsAdapter(regions) { region ->
bind(R.layout.view_region) { // this: ViewRegionBinding
title.text = region.title
description.text = region.description
}
}
```### Features
#### Context
Within `itemsAdapter` block you can use contextual data:
| Field | Description |
|--------------|---------------------------|
| `index: Int` | Index of the current item |#### Looping mode
Set parameter `isLooping` to `true` to use `ItemsAdapter` in looping mode.
It will only work if you have more than one element in data list.## Contributing
Merge requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.## License
[MIT][license]
[mavenCentral]: https://search.maven.org/search?q=g:com.redmadrobot.itemsadapter
[ci]: https://github.com/RedMadRobot/itemsadapter/actions
[license]: LICENSE[epoxy]: https://github.com/airbnb/epoxy/
[groupie]: https://github.com/lisawray/groupie/