⚡ A lazy tool to compose RecyclerView Adapter for Android ⚡
apply plugin: 'kotlin-kapt'
repositories {
jcenter()
}
implementation 'com.andrewjapar.redapter:redapter:0.1.0'
kapt 'com.andrewjapar.redapter:redapter-compiler:0.1.0'
Just simply create a class which implements the RedapterModel
data class UserInfo(val name: String): RedapterModel
sealed class UserInfo: RedapterModel {
data class Portrait(val image: String): UserInfo()
data class Landscape(val name: String): UserInfo()
}
Create a class which implements Redapter.ViewHolder
and annotate it with @BindLayout
. Then you have to override onBind
function in order to implement click listener and consume the data model.
@BindLayout(layout = R.layout.item_user_info, model = UserInfo.Portrait::class)
class PortraitUserInfoViewHolder(view: View) : Redapter.ViewHolder(view) {
override fun onBind(item: RedapterModel, itemActionListener: (RedapterModel) -> Unit) {
}
}
Add @BindViewHolder
annotation on your adapter variable, just like injecting something using dagger.
class MainActivity : AppCompatActivity() {
@BindViewHolder(UserInfo.Portrait::class, UserInfo.Landscape::class)
lateinit var adapter: Redapter.Adapter
override fun onCreate(savedInstanceState: Bundle?) {
Redapter.bind(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter.data = mutableListOf(UserInfo.Portrait("url"), UserInfo.Landscape("name"))
}
}
Just in case you want to create your own adapter class, you can use the same annotation @BindViewHolder
for the adapter class. Try to build the project and extend it with adapter class name with suffix _Helper
.
@BindViewHolder(UserInfo.Portrait::class, UserInfo.Landscape::class)
class MainAdapter : MainAdapter_Helper()
- Support View Bindings
- Pagination
- Please let me know, if you have any other ideas
Currently, redapter is used only on my personal project but if you have any idea to make it more powerfull, just make a pull request, You are in!
MIT License
Copyright (c) 2019 andrewjapar
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, publiAsh, 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.