Preface In the process of developing APP, engineers have to flirt with components such as ListView and GridView. The natural and original beauty BaseAdapter is the favorite of programmers. With it, we can do whatever we want. Hehe, you know O(∩_∩)O haha~ However, every time we write a BaseAdapter, we consciously write a ViewHolder for it. One or two is fine, but what if there are countless ListViews in the application? Hehe~ Damn! They are all the same, and it's aesthetically tiring. As the greatest programmers of the 22nd century, taking off and getting on are always our most sincere pursuits, so how do we take ViewHolder off from BaseAdapter? It's not that we don't use it, but we should make it into a gorgeous tool class implementation and put it into the lonely tools class in the corner.
ViewHolder Implementation I think I should briefly introduce the implementation of ViewHolder. Google cleverly used the idea of reusing View in Adapter, which naturally makes it possible for our loser phones to pick up some beautiful and rich girls. The specific implementation of ViewHolder is basically reflected in the getView(int position, View convertView, ViewGroup parent) method of BaseAdapter, see the following code:
Obviously, don't ask me where ViewHolder is. Just look up and you will see the big class ViewHolder. ViewHolder is used in two places. One is the reuse of convertView, and the other is the reuse of indexes in ViewHolder, which is convertView. If you are not familiar with the specific usage, you can search Baidu. If I go on, I will be sorry for my blog post today, because the purpose of writing this code here is definitely not to introduce how to use ViewHolder, but just to tell you: how bloated the traditional ViewHolder is! And for each new BaseAdapter, you have to implement it again and again, OH~ ViewHolder tool class implementation Of course, we should start stripping when we are young and take action as early as possible. Since we are tired of it, let's write it into a tool class. See the code below
This is the implementation of the tool class. Let me briefly explain the principle of implementation: 1. Since ViewHolder depends on the View's Tag storage, it is stored in a SparseArray collection. 2. Determine whether the Tag in the View exists in a viewHolder. If not, ask her to give birth to one immediately. 3. Then find the index of View in viewholder (that is, SparseArray). If it is not there, quickly findViewById and put it in and return it. If it already exists, everyone is happy and use it directly. Paste the code used in BaseAdapter:
It's concise and clear, no need to say more~~~ Hehe, if you want to write ViewHolder later, just call the Tools tool class directly, which saves you trouble and brain. . Analyze feasibility Since it is to be used as a tool, we need to first evaluate whether this tool is worth using. Generally speaking, we can evaluate from the following aspects: ease of use? Memory leaks? Performance improvement? Robustness? And so on. . . . . Ease of use: The biggest feature of the tool class is its simplicity and ease of use. The writing of this ViewHolder is a typical use-it-now approach. We don’t have to worry about writing some adaptation code at all. We can directly pass in the View and id, which is highly cohesive and loosely coupled. And it uses the generic template method of <T extends View> T to automatically adapt to the external View subclass, without us having to manually force the conversion. Memory leak: Some beginners will stubbornly believe that the SparseArray<View> viewHolder variable will have memory leaks when seeing static methods, but Java tells us that the life of this variable is only during the execution of the method. When the method is completed, the GC will recycle it; the ViewHolder is placed in the Tag of the View as always. Once the View is recycled, the ViewHolder will naturally disappear. If you don't believe it, open DDMS and try to refresh the listView with your 28-year-old hand speed to ensure that the object is basically stable at a value. Performance improvement: Here we find that the SparseArray collection is used instead of HashMap. We know that SparseArray is a tool class of Android and is officially recommended to replace HashMap<Integer,E>. It uses binary search internally to improve search efficiency. And it’s not just a little bit. Anyone who uses it knows it. If you want to know the specific content, you can go to Baidu Google or Zuozhuan source code. Therefore, as a tool class, it is fully qualified. Hurry up and copy it to your tools or utils, and then we can use ViewHolder happily and elegantly. |
<<: RxJava Operator Series 3 (Part 2)
>>: In-depth exploration of the essence of Android asynchronous Handler
These days, simply acquiring a large number of ne...
The college entrance examination scores of various...
"During the Qingming Festival, rain falls he...
[[405357]] Image source: https://pixabay.com/imag...
There are two types of production of Haikou Expre...
Review expert: Wang Lei, National Park and Nature...
Video description is the best option to display y...
The Internet has entered the second half of its e...
Dujiangyan Irrigation Project Located on the Minj...
At present, the most complete online search promo...
Today I will share with you some common tools for...
There are many wonderful animations such as child...
For people living in rural areas, many parents wo...
Google popularized the concept of self-driving ca...
A millionaire trader will teach you the core skil...