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.**
And what problem we want to avoid by using this solution?
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.
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
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
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.