As we all know, in the development process, the optimization of the ListView is more important
So, how to optimize it?
The improvement efficiency of the ListView is in fact the optimization of the GetView method in adapter, so how to make the GetView optimization?
1, the reuse of Convertview in GetView () , to a large extent, reduce the consumption of memory. By judging if Convertview is null,
Yes, you need to generate a view, then give the view data, and finally return the view to the bottom and present it to the user.
For example: The ListView screen shows 10 data, when the user slides the ListView, when the 11th data appears, the first piece of data
The recycler, which is provided by Android itself, puts the item (View) into the RecycleBin (Recycle Bin) and displays the new view
Image, repeat this view from the RecycleBin. There is no need to create new view every time, and save a lot of resources.
2, self write a viewhold class, this viewholder, used to identify some of the controls in the view, to facilitate the setting of the corresponding operation of some events ,
so you don't have to the View of GetView () just need to find the control once, not every time to find
The code is represented as follows:
Optimization of the GetView method
public View getView (int position, View Convertview, ViewGroup parent) {
if (Convertview = = null) {
Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (
r.layout.activity_list_item, NULL);
viewholder Viewholder = new Viewholder (Convertview);
Convertview.settag (Viewholder);
}
viewholder Viewholder = ( Viewholder) Convertview.gettag ();
Viewholder.fillview (GetItem (position));
return convertview;
}
This class is the display of the view, which is the display of each item in the ListView.
Class Viewholder {
Private ImageView iv_newsimage;
Private TextView Tv_newstitle;
Private TextView tv_newsdate;
Public Viewholder (View Convertview) {
Iv_newsimage = (ImageView) Convertview.findviewbyid (r.id.newimage);
Tv_newstitle = (TextView) Convertview.findviewbyid (r.id.newtitle);
Tv_newsdate = (TextView) Convertview.findviewbyid (r.id.newdate);
}
public void Fillview (Object item) {
Final Newsinfo news = (newsinfo) item;
Tv_newstitle.settext (News.getnewtitle ());
Tv_newsdate.settext (GetDate (News.getnewdate ()));
Imageloader.getinstance (). DisplayImage (News.getnewimageurl (),
Iv_newsimage);
}
Private String GetDate (long time) {
Date date = new Date (time);
SimpleDateFormat formatter = new SimpleDateFormat (
"Yyyy-mm-dd HH:mm:ss");
return Formatter.format (date);
}
}
How Android optimizes the ListView