Hello everyone.
In Android Development almost every project has one or more adapter classes to deal with a collection of views, also there are many different types of adapters such as RecyclerAdapter, ListAdapter, Paging2 Adapter, Paging 3 Adapter, ArrayAdapter, ExpandableListAdapter, some times you need to create a complex adapter class, but most of the times all you need is to bind your model class with a list item layout and provide some extra listeners and that's it, so why we canāt automate the process of creating those adapters classes?
Before talking about how we will automate it, letās first talk about what feature we want and what problems we want to avoid.
**
So letās start with the features.**
- Declare how to bind data to views easily.
- Add listeners to any view or to the main list item layout view.
- Support some features like loading images with third-party libraries like Picasso, Glide, COIL.
- Support default and custom names for the Adapter class.
- Provide clear error messages if we made any mistakes.
And what problem we want to avoid by using this solution?
- Add any cost to the runtime.
- Writing complex code to deal with this solution.
- Slow build time.
- Hard to learn.
- limitation.
So now what is this solution?
The solution isĀ EasyAdapterĀ which is a new Android Annotation Processing library that can generate adapter classes based on your model class and your custom information in the compile-time only.
So how does it work?
EasyAdapter has only 2 types of Annotations which are Adapters and Binds Annotations, the first type is used to annotate the model class to tell EasyAdapter what type of adapter you want to generate from this model and what list item you want to use for example
@ListAdapter("com.amrdeveloper.app", "list_item_model")
class Model
This example tells the library that you want to generate ListAdapter for the Model class and use `R.layout.list_item_model` as a list item, you also can change the generated name for example.
@ListAdapter("com.amrdeveloper.app", "list_item_model", "ModelAdapter")
class Model
And in version 1.0.0 EasyAdapter provides 6 Adapter types which are
- ArrayAdapter
- ListAdapter
- RecyclerAdapter
- PagedListAdapter
- PagingDataAdapter
- ExpandableListAdapter
You can also find full documentation about them with examples on the documentation website
The second annotation type is Bind Annotations and they used to bind or provide features
For example to tell EasyAdapter that you want to use this string field as a text for TextView with id R.id.user_name you can use @BindText
@BindText("user_name") val name : String
and this code will generate a code to load the image with path imagePath inside ImageView class with id `R.id.user_avatar`
@BindImage(ImageLoader.PICASSO, "user_avatar")
val imagePath : String
You can also use @BindListener annotation on the model class to generate listener for any view for example
@BindListener(ListenerType.OnClick)
@BindListener(ListenerType.OnClick, "call_button")
...
class Model
EasyAdapter now will generate 2 on click listener the first one for the list item itself and the other for view with id R.id.call_button
In Version 1.0.0 10 Bind Annotations which are
- BindText
- BindImage
- BindImageRes
- BindBackgroundColor
- BindBackgroundRes
- BindAlpha
- BindGif
- BindVisibility
- BindListener
- BindExpandable
- BindExpandableMap
You can also find full documentation about them with examples on the documentation website
Using EasyAdapter is not hard as you can see and you can learn it easily in minutes.
But How EasyAdapter really works?
EasyAdapter has support for 2 Annotation Processors__Kapt__Ā (Kotlin Annotation Processing Tool) andĀ
This is just the first version, and everyone is welcome to help to improve this tool by suggesting features, reporting issues and contributing to the code base or documentation, everyone can add value.
GitHub Repository:
Website:
You can find me on:Ā
Enjoy Programming š.
First published here.