listview
A view that showssitemsinaverticallyscrollinglist.
A list view that displays a vertical scrolling child item. In android development, listview is used in many places. It is used to display data into a vertical view. Using listview is a standard adapter mode, using data--, interface--xml and adapter--adapter. The data is displayed by the adapter in the required way. XML describes how the data is displayed, and these activities are controlled in the activity.
If you use a custom adapter, you will need to override the getView method, and generate the view and data for the user item in the getView method.
See the picture:
There is an optimization here, which is to reuse views, which reduces memory consumption and speeds up item loading.
Everyone must be in trouble when it comes to optimizing in getView. Below I have summarized three ways of optimizing. Please correct me.
First:
Reuse convertView, which greatly reduces memory consumption. By judging whether convertView is null, if so, you need to generate a view, then give the view data, and finally return the view to the bottom layer and present it to the user.
Features: If the current convertView is null, a view will be generated through LayoutInflat.
ViewCode publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { if(convertView==null) { convertView=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); } TextViewtv_name=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_name); TextViewtv_phone=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); ContactInfo1confo=contacts.get(position); if(confo!=null){//toseteveryitem'stext tv_name.setText(confo.getContactName()); tv_phone.setText(confo.getContact_Phone()); } returnconvertView; }
Second:
The above writing method has a disadvantage, that is, every time you getVIew, you need to findViewById again, find the control again, and then assign the value of the control and set the event accordingly. This is actually doing repetitive things, because geiview actually contains these controls, and the IDs of these controls are all the same. That is to say, you only need to find ViewById once in the view, and there is no need to find ViewById every time.
The second way of writing is given below.
The characteristics of writing are that there is usually an internal class classViewHolder. This ViewHolder is used to identify some controls in the view to facilitate the setting of corresponding operations for some events, such as onClick, etc. This eliminates the need for each You have to findViewById every time, which reduces performance consumption. At the same time, convertView is reused, which greatly reduces memory consumption.
ViewCode publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { ViewHolderholder; if(convertView==null){ convertView=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); convertView.setTag(holder); } else { holder=(ViewHolder)convertView.getTag(); } ContactInfo1confo=contacts.get(position); Log.i("my","confo"+confo.getContactName()); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } returnconvertView; } classViewHolder { TextViewtv_name,tv_phone; }
Third:
Personally, I think this way of writing is the most comfortable. The most comfortable way of writing means that it’s very refreshing to look at the code and it’s very clear.
Features: Use internal class classViewHolder and reuse convertView.
The difference between the second way of writing is that it uses a temporary variable Viewview=convertView, then modifies the view, and finally returns to the view
ViewCode @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { Viewview=convertView; ViewHolderholder; if(view==null){ view=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); view.setTag(holder); } else { holder=(ViewHolder)view.getTag(); } ContactInfo1confo=contacts.get(position); Log.i("my","confo"+confo.getContactName()); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } returnview; } classViewHolder { TextViewtv_name,tv_phone; }
The above is a centralized writing method for novices to learn and summarize.
The source code is as follows: LisViewTest.zip
According to the suggestions provided by friends downstairs, I found that there are still areas for optimization. The latest update is as follows:
ViewCode @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { Viewview=convertView; ViewHolderholder; if(view==null){ view=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); view.setTag(holder); } else { holder=(ViewHolder)view.getTag(); } ContactInfo1confo=contacts.get(position); Log.i("my","confo"+confo.getContactName()); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } returnview; } <fontcolor="\"#0000ff\""></font>staticclassViewHolder { TextViewtv_name,tv_phone; }
Note: staticclassViewHolder
Set ViewHolder to static here, which is static. Static classes will only It will take a long time to load for the first time, but it can help with loading later. At the same time, it ensures that there is only one ViewHolder in the memory, saving memory overhead.