For example, what would you do if you had a demand map like this now? As a beginner, before I was directly with the simpleadapter combination of an item layout to achieve, it is very convenient to implement (basically a line of code can be implemented), and did not feel anything bad. Until recently in the MU class online to see the "Robot Little Mu" and "Universal Adapter" two classes, only to Baseadapter know. After watching the course of the great God, I went online to search a few blogs, also read some source code and documents, so I intend to write a post to record their learning process.
In today's post, we start with the most basic implementation of the Baseadapter adapter, first introducing the ListView performance optimization (Convertview combined with the Viewholder Class), and then encapsulating it. Finally achieved can be like simpleadapter can be a line of code to fix a ListView data binding.
To summarize, the features that this post will implement:
- Add Viewholder to the original adapter, which inherits from Baseadapter, to achieve the function of caching
- Encapsulates the optimized ListView, implementing a line of code to bind data to the ListView
First, the implementation of the most primitive adapter class
The most primitive idea is a common adapter class that inherits from Baseadapter, finds the layout of the child view in the GetView () method, gets the control in the child view, and binds the data to it. The abbreviated code is as follows:
1 @Override2 PublicView GetView (intposition, View Convertview, ViewGroup parent) {3Convertview = Inflater.inflate (R.layout.sideworks_main_userlist_item, parent,false);4ImageView Userphoto =(ImageView) Convertview.findviewbyid (R.id.find_listitem_photo);5TextView UserName =(TextView) Convertview.findviewbyid (r.id.find_listitem_name);6User User =Userlist.get (position);7 Userphoto.setimageresource (User.getphotores ());8 Username.settext (User.getusername ());9 returnConvertview;Ten}
There is no problem in writing code in this way, but people who have read the source know that the Convertview parameter refers to the old view to reuse, if possible. that is to say, Convertview is a previously used sub-view, if it exists, it can be reused, that is, as long as there is a layout in the ListView and the child view of the same sub-view, then the child view can be reused this convertview. So there is a caching mechanism, that is, by this mechanism, we can achieve the goal of the ListView performance optimization.
Second, Convertview combined with Viewholder class to achieve ListView performance optimization
To be Continued ...
Android's ListView performance Optimization--Universal Adapter