https://github.com/commit451/aloy
Create a RecyclerView.Adapter without having to subclass
https://github.com/commit451/aloy
android kotlin recyclerview
Last synced: 11 months ago
JSON representation
Create a RecyclerView.Adapter without having to subclass
- Host: GitHub
- URL: https://github.com/commit451/aloy
- Owner: Commit451
- License: apache-2.0
- Created: 2017-08-21T20:19:59.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2022-06-27T04:22:32.000Z (over 3 years ago)
- Last Synced: 2025-04-04T07:04:28.487Z (11 months ago)
- Topics: android, kotlin, recyclerview
- Language: Kotlin
- Homepage:
- Size: 1.3 MB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Aloy
[](https://jitpack.io/#Commit451/Aloy)
Create a RecyclerView.Adapter without having to subclass.
## Usage
The idea is to be able to have a valid `RecyclerView.Adapter` without having to create a class for the adapter. We do this by providing lambdas to call for `onCreateViewHolder` and `onBindViewHolder`:
```kotlin
//top of file
lateinit var adapter: AloyAdapter
//later, in onCreate (Activity) or onViewCreated (Fragment) for example:
adapter = AloyAdapter(
onCreateViewHolder = { parent, viewType ->
val holder = CheeseViewHolder.inflate(parent)
holder.itemView.setOnClickListener {
val cheese = adapter.items[holder.adapterPosition]
Snackbar.make(root, "${cheese.name} clicked", Snackbar.LENGTH_SHORT)
.show()
}
holder
},
onBindViewHolder = { viewHolder, position, item ->
viewHolder.bind(item)
}
)
```
You can also set the `onCreateViewHolder` and `onBindViewHolder` lambdas after construction, just make sure you do so before the first call to `onCreateViewHolder` or `onBindViewHolder`. This is useful for subclassing.
See the sample `app` module for more usage.
## Optional
You can optionally override `getItemViewType` if you need to by setting the `onGetItemViewType` on the adapter.
License
--------
Copyright 2022 Commit 451
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.