kiel4.7下载_使用Kiel构建更好,干净的RecyclerView.Adapter
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開發人員最受歡迎的答案之一。
Photo by T. Q. on Unsplash TQ在Unsplash上拍攝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 viewtypes. 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)
Photo by Jeremy Thomas on Unsplash 杰里米·托馬斯 ( Jeremy Thomas)攝影: UnsplashKiel 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:)
演示地址
演示地址
You can find Kiel:
您可以找到基爾:
You can download:
您可以下載:
implementation 'me.ibrahimyilmaz:kiel:latestVersion'Your ideas/feedbacks/PRs/Issues are greately appreciated.
非常感謝您的想法/反饋/ PR /問題。
Happy Coding!
編碼愉快!
翻譯自: https://medium.com/swlh/build-better-and-clean-recyclerview-adapter-with-kiel-a129882c1e1
kiel4.7下載
總結
以上是生活随笔為你收集整理的kiel4.7下载_使用Kiel构建更好,干净的RecyclerView.Adapter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse下载不了,无法访问此网页,
- 下一篇: LS-WXL/E改机应用流程全攻略(改机