Android:listview caching mechanism and the baseadapter of the triple State (tease, ordinary, literary style)

Source: Internet
Author: User
Tags log log

We all know that the ListView format is certain, and the data source is indeed multiple and diverse, this time you need an adapter to convert the data source to the format that the ListView will display

Baseadapter was born.

The display and caching mechanisms of the ListView and GridView are as follows

We all know that the size of the screen is limited, but the data in the ListView can be a lot so the phone can't show all the data at once. It only loads the data displayed on the screen.

For example, when we slide the screen down, the item1 is recycled to recycler and ITEM8 to be displayed on the screen item8 remove such a layout file from recycler and reset the data to be displayed by ITEM8 and set the location to be displayed. After all, a word needs to be recovered to the cache after the display is displayed.

As for the question, why is the difference between the threefold realm of baseadapter (tease, ordinary, literary)? Just how to use the display and caching mechanism of the ListView

There is such a method on the adapter of the ListView

@Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubreturn N ull;}

The difference between the ordinary literary style of the tease is how to use this method

Let's compare the triple realm of adapter with an example to be strong and weak.

The effect to be achieved is as follows


Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Fill_ Parent "    android:layout_height=" fill_parent "    android:orientation=" vertical ">    <listview         Android:id= "@+id/lv_listview"        android:layout_width= "match_parent"        android:layout_height= "Match_parent"        ></ListView></LinearLayout>

Item.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <imageview Andro Id:id= "@+id/iv_image" android:layout_width= "50DP" android:layout_height= "50DP" Android:layout_alignpa        Rentleft= "true" android:src= "@drawable/ic_launcher"/> <textview android:id= "@+id/tv_nickname" Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_torightof= "@id/iv_ Image "Android:gravity=" Center_horizontal "android:textsize=" 20sp "android:text=" nickname "/> <tex TView android:id= "@+id/tv_content" android:layout_width= "fill_parent" android:layout_height= "Wrap_con Tent "android:layout_torightof=" @id/iv_image "android:layout_below=" @id/tv_nickname "android:textsize = "15SP" android:text= "content"/></relativelayout> 

For convenience, I created a MyObject object to encapsulate the data item.xml to display

Myobject.class

Package Com.example.baseadapter;import Android.widget.imageview;import Android.widget.textview;public class MyObject {private int imageviewid;private string Nickname;private string content;public MyObject (int imageviewid, string Nickname, String content) {This.imageviewid = Imageviewid;this.nickname = Nickname;this.content = content;} Public String Getnickname () {return nickname;} Public String getcontent () {return content;} public int Getimageviewid () {return imageviewid;}}

Mainactivity.class
Package Com.example.baseadapter;import Java.util.arraylist;import Java.util.list;import android.os.Bundle;import Android.app.activity;import Android.view.menu;import Android.widget.listview;public class MainActivity extends Activity {private list<myobject> list=new arraylist<myobject> ();p rivate ListView listview;private Myadapter myadapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (R.layout.activity_main); listview= (ListView) Findviewbyid (R.id.lv_listview); for (int i=0;i<50;i + +) {List.add (new MyObject (R.drawable.ic_launcher, "nickname" +i, "Content" +i));} Myadapter=new Myadapter (list, this); Listview.setadapter (Myadapter);}}

Custom Adapter

Myadapter.class

Package Com.example.baseadapter;import Java.util.list;import Android.content.context;import android.util.Log; Import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;public class Myadapter extends Baseadapter {private long sum = 0 ;p rivate list<myobject> list;private context context;public myadapter (list<myobject> List, Context context {this.list = List;this.context = Context;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn list.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn list.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method stub//tease L Ong star = System.nanotime (); View view=view.inflate (context, r.layout.item, null); ImageView Imageview = (ImageView) View.findviewbyid (r.id.iv_image); TextView nickname= (TextView) View.findviewbyid (r.id.tv_nickname); TextView content= (TextView) View.findviewbyid (r.id.tv_content); MyObject Myobject=list.get (position); Imageview.setbackgroundresource (Myobject.getimageviewid ()); Nickname.settext (Myobject.getnickname ()); Content.settext (Myobject.getcontent ()); Long end = System.nanotime (); sum + = End-star; LOG.D ("main", "tease" + sum); Return view;//normal//long star = System.nanotime ()//Get system nanosecond time//if (Convertview = = null) {//convertview = View.inflate (cont EXT, r.layout.item, null);//}//imageview ImageView = (ImageView) Convertview//.findviewbyid (r.id.iv_image);// TextView nickname = (TextView) Convertview//.findviewbyid (r.id.tv_nickname);//textview content = (TextView) Convertview.findviewbyid (r.id.tv_content);//myobject MyObject = list.get (position);// Imageview.setbackgroundresource (Myobject.getimageviewid ());//nickname.settext (Myobject.getnickname ());// Content.settext (Myobject.getcontent ();//long end = System.nanotime ();//sum + = END-STAR;//LOG.D ("main", "normal" + sum);//return convertview;//literary style//Long star  = System.nanotime ();//Viewholder viewholder;//if (Convertview = = null) {//Viewholder=new viewholder ();//Convertview = View.inflate (context, r.layout.item, NULL);//Viewholder.imageview = (ImageView) convertview//. Findviewbyid (r.id.iv_ image);//Viewholder.nickname = (TextView) convertview//. Findviewbyid (R.id.tv_nickname);//Viewholder.content = ( TextView) convertview//Findviewbyid (r.id.tv_content)/////////////////////Settag by Viewholder Convertview.settag (Viewholder);//}//viewholder= (Viewholder) Convertview.gettag ();//MyObject MyObject = List.get ( position);//ViewHolder.imageView.setBackgroundResource (Myobject.getimageviewid ());// ViewHolder.nickName.setText (Myobject.getnickname ());//ViewHolder.content.setText (Myobject.getcontent ());//Long End = System.nanotime ();//sum + = end-star;//log.d ("main", "literary Style" +sum);//return convertview;} Avoid duplicates of findViewbyidclass viewholder {public ImageView imageview;private TextView nickname;private TextView content;}} 

Baseadapter's Triple Realm (tease, plain, literary) code is already on top

Let's analyze the first reason why it's called the tease.

As we have already said, Liseview has a caching mechanism to put the used item into the buffer pool, and every time we call the GetView method, we create a new view object without using the cache mechanism of the ListView.

Without any processing, efficiency and inefficiency, so we call it a tease.

The second common formula uses the Contentview object passed in the GetView method and adds an if judgment

if (Convertview = = null) {Convertview = View.inflate (context, r.layout.item, null);}
Although it is only an if judgment, this judgment avoids the creation of a large number of Contentview objects. Save a lot of time using the cache feature of the ListView if there is no cache to create a new view the GetView method is very well optimized but this is just getting started, Because Findviewbyid still wastes a lot of time, so we call this a normal type of method.

Then the third realm of literary style is the optimization of the Findviewbyid to put Findviewbyid also put in if judgment statement is OK

We need to create an inner class with three member variables that are the three controls in our Item.xml file

Class Viewholder {public ImageView imageview;private TextView nickname;private TextView content;}
Viewholder and Contentview are associated with the Viewholder.settag () method and are viewholder by the Gettag method

This method not only utilizes the cache of the ListView, but also realizes the cache of the display view through Viewholder, avoids using the Findviewbyid method multiple times.

As a sentimental programmer, this is the most literary writing ~

You may have seen my code in the output of the log log below I enclose the results everybody compare here only 50 data in the ListView are all slipping down the bottom



At this point baseadapter the triple realm (tease, ordinary, literary style) which is strong or weak at a glance ~ (if the discrepancy please set more data in the ListView)


Reference: MU class net

Android:listview caching mechanism and the baseadapter of the triple State (tease, ordinary, literary style)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.