RecyclerView Adapter with Anko
Anko Layouts has already proved that it can be used anywhere to design Android app layouts without XML. However, it lacks a complete documentation to fully engage with it. I am writing this story to share my experience about the construction of RecyclerView Adapter with Anko.
Let me assume you have created a new project on Android Studio and set up everything ready to work.
Step 1. Add Anko dependencies to your app level gradle file:
implementation "com.android.support:recyclerview-v7:27.1.1"
implementation "org.jetbrains.anko:anko-sdk15:0.10.5"
implementation "org.jetbrains.anko:anko-appcompat-v7:0.10.5"
implementation "org.jetbrains.anko:anko-recyclerview-v7:0.10.5"
Step 2. Create a simple data class in Kotlin, for example:
Step 3. Create a new layout using Anko Layouts DSL:
You may be wondering why view ids are constant and not given at runtime. There are some other blogs that suggest you to give ids using ids.xml:
You may use the ids.xml, but my goal was to be independent from any XML and avoid a museum of ids (which will be hard to maintain later).
Step 4. Create a simple recycler view adapter:
Step 5. Finally, create some movie objects and use the adapter:
The result would be similar to this:
Thank you for reading this article. If you find it useful, clap 👏 and help others to find this article.
The full source code is here: