This repository was archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Adapter Generation
Kaustubh Patange edited this page Aug 23, 2021
·
2 revisions
- RecyclerViewAdpater
- ListAdapter (recommended, check sample-ktx)
Read these tips to understand more about this processing.
- Supports standard
RecyclerView.Adapter&ListAdaptergeneration. - Support for
OnClick&OnLongClickmethods. - Supports
DiffUtil.ItemCallbackgeneration method. - Support to load images through
Glide. - Support for multiple view types, check sample-ktx.
- Custom Linter to check incorrect implementation & auto fix it.
- More?
implementation 'io.github.kaustubhpatange:autobindings-recyclerview:<version>'
// Kotlin
apply plugin: 'kotlin-kapt' // at top of your module build.gradle file
kapt 'io.github.kaustubhpatange:autobindings-compiler:<version>'
// Java
annotationProcessor 'io.github.kaustubhpatange:autobindings-compiler:<version>'There might be some breaking changes in future alpha releases.
- A basic adapter consisting of a
dataSet& are updated throughadapter.notify*methods, usually used for the normal purposes.
@RecyclerViewAdapter(Data::class)
class TestAdapter {
@GlideLoadArray(
GlideLoad(R.id.image_id, "parameter-name", ...)
)
@OnBind(R.layout.item_layout_first)
fun bind1(view: View, item: Data, position: Int) {
// Set your views for first layout.
}
@OnBind(R.layout.item_layout_second, viewType = 2)
fun bind2(view: View, item: Data, position: Int) {
// Set your views for second layout.
}
@OnClick(R.id.item_id, viewType = 2)
fun onClick(context: Context, item: Data, position: Int) {
// OnClick for only second layout.
}
@OnLongClick(R.id.item_id)
fun onLongClick(context: Context, item: Data, position: Int) {
// OnClick for only first layout.
}
@ItemViewType
fun viewType(position: Int): Int = 0
}- Compile the project and you'll have
BindTestAdapterclass generated (ready for use). - You can then use the
BindTestAdapterclass withrecyclerView.
// Kotlin
recyclerView.adapter = BindTestAdapter(TestAdapter(), List<Data>)
//Java
recyclerView.setAdapter(new BindTestAdapter(new TestAdapter(), List<Data>));- Notice we're passing a new instance of
TestAdapteras parameter because we don't want to allocate objects for every instance ofBindTestAdaptereven if we are not using it.
- A modern way of writing adapter supporting
diffutilcallbacks.
@RecyclerViewListAdapter(Data::class)
class TestAdapter {
@DiffItemSame
fun itemSame(oldItem: Data, newItem: Data): Boolean {
// DiffUtil.areItemsTheSame callback
}
@DiffContentSame
fun contentSame(oldItem: Data, newItem: Data): Boolean {
// DiffUtil.areContentsTheSame callback
}
...
}- Compile the project and you'll have
BindTestAdapterclass generated (ready for use). - You can then use the
BindTestAdapterclass withrecyclerView.
- If you pass
setInViewHolder = falsein@OnClickor@OnLongClick, then the clicks will be generated inonBindViewHolderinstead. This is useful if your listeners are dynamic (i.e your view updates are frequent). - You can set click listener for specific
viewType, default is firstOnBind(). - A custom linter is provided within the library to handle specific compile-time errors.