Android's listview cache mechanism

Source: Internet
Author: User

Activity_main.xml:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.day_05_06.MainActivity" >


<listview
Android:id= "@+id/lvgenerals"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:divider= "#ccc"
android:dividerheight= "20DP"/>


</RelativeLayout>

Mainactivity.java:

Package com.example.day_05_06;


Import java.util.ArrayList;
Import java.util.List;


Import com.litsoft.General.General;


Import android.support.v7.app.ActionBarActivity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Adapter;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;


public class Mainactivity extends Actionbaractivity {
Private ListView Lvgenerals;//listview Object
Private list<general> Generals;//Data collection
Private Baseadapter adapter;//Adapter
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
InitData ();
Initview ();
}
private void Initview () {
TODO auto-generated Method Stub
Lvgenerals = (ListView) Findviewbyid (r.id.lvgenerals);
adapter = new Generaladapter (this,generals);
Lvgenerals.setadapter (adapter);
}
private void InitData () {
TODO auto-generated Method Stub
Generals = new ArrayList ();

int [] images = new int[] {
R.drawable.baiqi,r.drawable.caocao,r.drawable.chengjisihan,
R.drawable.hanxin,r.drawable.lishimin,r.drawable.nuerhachi,
R.drawable.sunbin,r.drawable.sunwu,r.drawable.yuefei,
R.drawable.zhuyuanzhang
};
String [] names = Getresources (). Getstringarray (R.array.generals);
for (int i=0;i<images.length;i++) {
General general = new General (Images[i],names[i]);
Generals.add (General);
}
}
Class Generaladapter extends baseadapter{
Private context context;
Private list<general> innergenerals;
Public Generaladapter (context context, list<general> innergenerals) {//The context object and the General collection object of the external class are passed here in the form of a constructor
Super ();
This.context = context;
This.innergenerals = innergenerals;
}

@Override
public int GetCount () {
TODO auto-generated Method Stub
return Generals.size ();
}





@Override
Public General GetItem (int position) {
TODO auto-generated Method Stub
return Generals.get (position);
}


@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}


@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
LOG.I ("main", position+ "");
Viewholder viewholder = null;

if (Convertview = = null) {//When a list item has not been created in the first screen
Viewholder = new Viewholder ();
Convertview = view.inflate (context, r.layout.general, null);
Viewholder.ivgerneral = (ImageView) Convertview.findviewbyid (R.ID.IVTHUMB);
Viewholder.tvname = (TextView) Convertview.findviewbyid (r.id.tvname);
Convertview.settag (Viewholder);//This is equivalent to initializing a layout for adaptation at a time, which is used in the future, that is, when scrolling, it is not necessary to initialize the layout for adaptation.
}else{//after scrolling, other list items appear
Viewholder = (Viewholder) convertview.gettag ();
}
General general = This.innerGenerals.get (position);
ViewHolder.ivGerneral.setImageResource (GENERAL.GETIMAGESRC ());
ViewHolder.tvName.setText (General.getname ());
return convertview;
}

}
Class viewholder{//is used to fit the layout of a specific item
ImageView ivgerneral;
TextView Tvname;
}


}

Matching page layouts: general.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >

<imageview
Android:id= "@+id/ivthumb"
Android:layout_width= "60DP"
android:layout_height= "60DP"
android:src= "@drawable/baiqi"/>
<textview
Android:id= "@+id/tvname"
Android:layout_width= "Match_parent"
android:layout_height= "60DP"
android:text= "White"
/>
</LinearLayout>

General.java:

Package com.litsoft.General;


public class General {
private int imagesrc;
private String name;
public int getimagesrc () {
return imagesrc;
}
public void setimagesrc (int imagesrc) {
THIS.IMAGESRC = IMAGESRC;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public General (int imagesrc, String name) {
Super ();
THIS.IMAGESRC = IMAGESRC;
THIS.name = name;
}

}

Strings.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>


<string name= "App_name" >Day_05_06</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Action_settings" >Settings</string>
<string-array name= "Generals" >
<item> White </item>
<item> Caocao </item>
<item> Genghis Khan </item>
<item> Han </item>
<item> Li Shimin </item>
<item> Noor Nurhachi </item>
<item> Sun Bin </item>
<item> Sun Wu </item>
<item> Zhu Yuanzhang </item>
<item> Yue Fei </item>
</string-array>
</resources>

Effect:

650) this.width=650; "src=" http://img.blog.csdn.net/20141201141918770 "/>

This article from "Follow the heart of the other side" blog, declined reprint!

Android's listview cache mechanism

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.