Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

ViewBindingAdapter

CraZyLegenD edited this page Sep 5, 2020 · 5 revisions

This annotation is responsible for the whole generation of the ListAdapter

@ViewBindingAdapter
(
val viewBinding: KClass<*>, 
val attachItemViewClickListener: Boolean = true, 
val attachItemViewLongClickListener: Boolean = false,
val generateViewBindingStaticNames: Boolean = false
)

The constructor for this annotation takes one mandatory field and three optional

val viewBinding: KClass<*>, //the view binding class -->[MANDATORY]<--
val attachItemViewClickListener: Boolean = true, //click listener attachment to the itemView -->[OPTIONAL]<--
val attachItemViewLongClickListener: Boolean = false //long click listener attachment to the itemView -->[OPTIONAL]<--
val generateViewBindingStaticNames: Boolean = false //creates static class for your view binding so that you shouldn't paste the names around -->[OPTIONAL BUT IT SHOULD BE USE TO AVOID COPY PASTING STRINGS SINCE IT CAN LEAD TO ]<-- 

The val viewBinding: KClass<*> is taking a Class that extends ViewBinding class, for example ItemViewPersonBinding:class.

This class will be used as a constructor for the view holder parameter which will be generated for you automatically if you don't annotate a class inside your pojo model with @ViewHolderBinding, if you use this annotation you also get the same binding as a constructor for that class.

If your cases can't be satisfied with all the binding annotations and you have more logic going inside the view holder, you can take a look at how to provide a custom view holder.

When your pojo data model is a data class MyModel and you don't annotate a class inside your pojo model with @DiffUtilBinding, you get out of the box diff util generated for you, leveraging the ability the data class provides since behind the scenes it generates equals methods, you can read more here, but if your logic is more complex when you're dispatching the diff util to the adapter you can provide a custom diff util callback.

If you don't provide a custom view holder the view holder will be generated inside the adapter class and all the binding options will be applied to the view binding properties.


val attachItemViewClickListener: Boolean = true

This option generates a click listener for your view holder's itemView naming it onItemViewClickListener.


val attachItemViewLongClickListener: Boolean = true

This option generates a long click listener for your view holder's itemView naming it onLongItemViewClickListener.


val generateViewBindingStaticNames: Boolean = true

This option generates an object class with the view binding names as a static variables so that you shouldn't copy paste Strings around due to a chance of missing a character etc..

For example if you use a class called ItemviewPersonBinding there's gonna be an object with the name ItemviewPersonBindingNames generated for you, containing all the view binding names.

In your app's gradle inside the android closure add

sourceSets {
        main {
            java {
                srcDir "${buildDir.absolutePath}/generated/source/kaptKotlin/"
            }
        }
    }

If you don't add this option you won't be able to use the statically generated names, ignore if you don't want to use them.

Later on you can bind your view like this

@BindText(ItemviewPersonBindingNames.title) val name: String,

@BindText(ItemviewPersonBindingNames.content, clickListenerType = ClickListenerType.LONG_CLICK) val surname: String

The click listener is generated in your pojo model in form of an interface and is responsible for delegating the clicks, once the adapter is generated you can simply use (there's a bug that breaks other things in Kotlin's kapt 1.4.0 so for now you must use camelCase as your variable names, for now!).

myPojoModelAdapter.onItemViewClickListener = MyPojoModelAdapter.forItemClickListener { position, item, view -> Log.d("CLICKED AT $position", "LONG ITEM VIEW CLICK ${item.name} at view ${view.id}") }

If you attached click listeners to the bindings you get them as surnameLongClickListener or nameItemClickListener assuming you attached the surname property with long click listener and the name property with click listener.


Clone this wiki locally