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

Custom Diff Util

CraZyLegenD edited this page Aug 26, 2020 · 2 revisions

The default diff util didn't satisfy your needs?

You can create custom one that'll do the job for you.

The custom diff util must be a class member of your pojo (data) model!!!

Let's take this example

@ViewBindingAdapter(ItemviewPersonBinding::class)
data class Person(
    @BindText("title", clickListenerType = ClickListenerType.CLICK) val name: String,

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


    @DiffUtilBinding
    class PersonDiffUtl : DiffUtil.ItemCallback<Person>(){
        override fun areItemsTheSame(oldItem: Person, newItem: Person): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: Person, newItem: Person): Boolean {
            return oldItem.name.length > newItem.name.length
        }
    }

}

Your custom diff util has to extend DiffUtil.ItemCallback and take the type parameter as your pojo(data) model, then you implement the necessary classes and voila, that's it.

You have your custom diff util for your adapter.

Clone this wiki locally