kiel4.7下载
Some years ago, we had only ListView and its Adapter. It had performance issues and these issues were resolved with ViewHolder Pattern (Here also I want to send my greatest appreciation to the one who found and shared this).
几年前,我们只有ListView及其适配器。 它存在性能问题,这些问题已通过ViewHolder Pattern解决(在此,我还要向发现并分享此内容的人表示最大的感谢)。
If we go back and recall it in detail, it is basically wrapping some view related software entities into ViewHolder class, saving it in View’s tag and using it in. Hopefully, Google released better ListView, which is RecyclerView, for us.
如果我们回过头来详细回顾一下,它基本上是将一些与视图相关的软件实体包装到ViewHolder类中,将其保存在View的标签中并在其中使用。希望Google可以为我们发布更好的ListView,即RecyclerView。
As we are in the mud of Android Hell (lifecycle problems, architecture problems, UI development problems etc etc.), most of us like did not notice the adapter hell in ListView. We did not complain about it enough as we did in this ViewHolder pattern, so Google has not saved us from this hell yet.
由于我们陷入了Android Hell的泥潭(生命周期问题,体系结构问题,UI开发问题等),我们大多数人都没有注意到ListView中的适配器地狱。 我们没有像在ViewHolder模式中那样对它进行抱怨,因此Google尚未使我们摆脱困境。
什么是适配器地狱? (What is Adapter Hell?)
Writing/Duplicating the same RecyclerView.Adapter implementation for each Fragment/Activity which uses RecyclerView to visualize the data. Code Duplication is bad but duplicating bug or limitation is worser thing that we can do for us🤢🤢🤢.
为每个使用RecyclerView可视化数据的Fragment / Activity编写/复制相同的RecyclerView.Adapter实现。 代码复制是不好的,但是复制错误或限制是我们可以为我们做的更糟糕的事情。
In Twitter, there was a confession post-cycle for Android Developers and Duplicating RecyclerView.Adapter is the one of the most popular answers by Android Developers.
在Twitter中,有一个供Android开发人员使用的告白后循环,重复RecyclerView.Adapter是Android开发人员最受欢迎的答案之一。
If there are more than one type to be visualized in RecyclerView, it means more wood in the fire of this hell 🥵.
如果要在RecyclerView中可视化一种以上的类型,则意味着此地狱火中的木材更多。
Because it means that you will have:
因为这意味着您将拥有:
- more if/when/switch statement 😥 更多if / when / switch语句😥
- more casting 🤯 更多铸造🤯
- more bug🤬 更多错误🤬
There are many very good solutions and articles about these issues.
关于这些问题,有很多很好的解决方案和文章。
One of the most popular idea is based on Delegation Pattern over RecyclerView.Adapters, which is basically we create a generic adapter, we have delegation for each type which help us to create ViewHolder and binding data to ViewHolder.
最受欢迎的想法之一是基于RecyclerView.Adapters上的委派模式,这基本上是我们创建一个通用适配器,每种类型都有委派,这有助于我们创建ViewHolder并将数据绑定到ViewHolder。
You should absolutely read Hannes Dorfmann’s Adapter Hell Escape Article. I want to say thanks to him for such a great article and AdapterDelegates library. It still helps a lot.
您应该绝对阅读Hannes Dorfmann的Adapter Hell Escape文章。 我要感谢他的出色文章和AdapterDelegates库。 它仍然有很大帮助。
Another pain point is the viewtype
s. It should be unique arbitrary Integer
. Our data types in the adapter are unique, so we have different data type for different views. But we have to define this Integer
value😢.
另一个痛苦点是viewtype
s。 它应该是唯一的任意Integer
。 适配器中的数据类型是唯一的,因此对于不同的视图,我们具有不同的数据类型。 但是我们必须定义此Integer
😢。
Should we define them as 1,2,3 or 4?
我们应该将它们定义为1,2,3还是4?
It makes me remember button1
, button2
, button3
…
这让我想起button1
, button2
, button3
…
Unfortunately ,we can’t use that data types hash value because HashValue is integer. There can be collision!😳
不幸的是,我们不能使用该数据类型的哈希值,因为HashValue是整数。 可能会发生碰撞!😳
Don’t worry there is also a solution for that, which is basically using layout resource as a viewType
! It is unique, it is logically connected with both ViewHolder
and data type
.
不用担心,还有一个解决方案,它基本上是将布局资源用作viewType
! 它是唯一的,它在逻辑上与ViewHolder
和data type
。
You should absolutely read Danny Preusler’s article about that.
您应该绝对阅读Danny Preusler的文章。
So the idea:
所以这个想法:
Don’t copy/paste/write same adapter code for each
RecyclerView
.不要为每个
RecyclerView
复制/粘贴/编写相同的适配器代码。Use your layout resource as a
viewType
.将布局资源用作
viewType
。
RecyclerView.Adapter与基尔 (RecyclerView.Adapter with Kiel)
Kiel is designed to be easy to integrate, easy to use based on these two ideas with the help of the power of Kotlin💪.
在Kotlin💪的帮助下,基于这两个想法, Kiel易于集成,易于使用。
It enables us to configure our adapter with existing vocabulary of RecyclerView.Adapter
. So we don’t need to learn new concepts to implement/ handle the things related to RecyclerView.Adapters, such as binding ViewHolder, handling events like onClick
,onLongClick
or something else.
它使我们能够使用RecyclerView.Adapter
现有词汇来配置适配器。 因此,我们无需学习新概念即可实现/处理与RecyclerView.Adapters相关的事情,例如绑定ViewHolder,处理诸如onClick
, onLongClick
类的事件。
So when we check how Kiel helps us:
因此,当我们检查基尔如何帮助我们时:
RecyclerView.Adapter实现变为: (RecyclerView.Adapter Implementation becomes:)
处理不同的视图类型变为: (Handling Different View Types becomes:)
差异变为: (Diffing becomes:)
与有效负载差异: (Diffing with Payloads:)
翻译自: https://medium.com/swlh/build-better-and-clean-recyclerview-adapter-with-kiel-a129882c1e1
kiel4.7下载