lisawray / groupie

Groupie helps you display and manage complex RecyclerView layouts.
MIT License
3.67k stars 293 forks source link

Force to reuse same ViewHolder with stableIds #258

Open ATizik opened 5 years ago

ATizik commented 5 years ago

Is it possible to force GroupAdapter to reuse the same ViewHolder for an Item with stable ids enabled?

Right now for a simple recycler with match_parent, fixedSize and stableIds enabled two ViewHolders for a single Item are created. Which means that typing in an EditText with TextWatcher that updates underlying models is unreliable, in practice it produces flickering between two views, which either drops cursor or doesn't update EditText text properly depending on implementation.

For static pages this works:

view.setViewCacheExtension(object : RecyclerView.ViewCacheExtension() {
                    override fun getViewForPositionAndType(recycler: RecyclerView.Recycler, position: Int, type: Int): View? {
                        if (type == getItemViewType(position))
                            return (getItem(position)).let {
                                viewCache[it.id]
                            }
                        return null
                    }
                })
class StaticGroupAdapter:GroupAdapter<ViewHolder>(){
    val viewCache: MutableMap<Long,View> = mutableMapOf()

    override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)
        viewCache[holder.itemId] = holder.itemView
    }

    override fun onViewRecycled(holder: ViewHolder) {
        super.onViewRecycled(holder)
        viewCache.remove(holder.itemId)
    }

}

Maybe there is a more elegant solution, which also works for dynamic pages where items aren't fixed in place?

ValCanBuild commented 5 years ago

@ATizik would you mind providing an example project that showcases the issue you are having. This will help me figure it out.