DEV Community

Cover image for How to create Adapters easily in 2022 as an Android Developer
Amr Hesham
Amr Hesham

Posted on

How to create Adapters easily in 2022 as an Android Developer

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, lets 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 a 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 KSP (Kotlin Symbol Processing) to walk in your source code and collect information from the annotations with simple type checking to make sure for example what @BindImage annotate string field which represent the image path, show error and warn messages if needed, and after we have all the required information we used code generators that generate the result adapter class source code using kotlinpoet library from Square then we write this source code in Adapter files inside the output directory:D. we try to make the generated code as optimized as possible for example we avoided calling mutable findViewById with the same id, Instead of that we have a ViewTable class inspired from the SymbolTable concept to manage the declared variable and reuse them again.

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 Repositroy: AmrDeveloper/EasyAdapter
Website: EasyAdapter

Enjoy Programming šŸ˜‹.

Top comments (0)