mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.84k stars 494 forks source link

Setting a String identifier on AbstractItem #833

Closed mikezliu closed 4 years ago

mikezliu commented 4 years ago

AbstractItem requires that the identifier is a long. I'm using unique uid for my server side items, and converting them to a long may result in collisions.

Is there a way to specify a string identifier?

I'm currently converting the server side uuid to a long identifier by using hashCode of the string, but this can collide so it's not a great solution. I need to use identifier to avoid blinking issues when using FastAdapterDiffUtil

mikepenz commented 4 years ago

@mikezliu sadly not. RecyclerViews require a long identifier. This is not a limitation of this library.

An alternative is to use the idDistributor but you will loose the possibility to identify the items by your identifier directly, you'd need a different strategy to match them with the key then.

I tend to use the following which proved quite reliable:

/**
 * Creates a 64 bit hash string from the given charsequences
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
fun CharSequence.to64BitHash(): Long {
    var result = -0x340d631b7bdddcdbL
    val len = this.length
    for (i in 0 until len) {
        result = result xor this[i].toLong()
        result *= 0x100000001b3L
    }
    return result
}

existence of an overlap exists still, but the chance it extremely low

mikezliu commented 4 years ago

@mikepenz - I'm used to using DiffUtil with recycler views, and I don't think there is such a limitation there (the DiffUtil callback lets you compare two objects for equality however you want, and I can do a string comparison there - https://developer.android.com/reference/android/support/v17/leanback/widget/DiffCallback). Is the limitation you're referring to in RecyclerViews just for non DiffUtil usages? I'm not that familiar there so not sure which limitation you're referring to.

mikepenz commented 4 years ago

You are mixing up 2 different things.

The diffUtil provides a way to detect differences of item in a very generic way. wheras the identifier is meant to be a unique id used to optimize various things like animations inside the RecyclerView

See: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#setHasStableIds(boolean) and: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemId(int)

mikezliu commented 4 years ago

@mikepenz - ah makes sense, thanks for the clarification