码迷,mamicode.com
首页 > 移动开发 > 详细

android之listView缓存机制

时间:2014-12-07 06:48:35      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:android

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对象
private List<General> generals ;//数据集合
private BaseAdapter 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) {//这里用构造器的方式传递外部类的上下文对象和General集合对象
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){//第一屏中尚未创建列表项时
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);//这里相当于初始化一次用于适配的布局,以后都用这个布局了,就是滚动的时候就不用再初始化用于适配的布局
}else{//以后的滚动,出现其他的列表项
viewHolder = (ViewHolder) convertView.getTag();
}
General general = this.innerGenerals.get(position);
viewHolder.ivGerneral.setImageResource(general.getImageSrc());
viewHolder.tvName.setText(general.getName());
return convertView;
}

}
class ViewHolder{//用于适配布局的具体项
ImageView ivGerneral;
TextView tvName;
}


}

适配的页面布局: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="白起"
    />
</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>白起</item>
   <item>曹操</item>
   <item>成吉思汗</item>
   <item>韩信</item>
   <item>李世民</item>
   <item>努尔哈赤</item>
   <item>孙膑</item>
   <item>孙武</item>
   <item>朱元璋</item>
   <item>岳飞</item>
</string-array>
</resources>

效果:

bubuko.com,布布扣

本文出自 “追随心的彼岸” 博客,谢绝转载!

android之listView缓存机制

标签:android

原文地址:http://qa962839575.blog.51cto.com/6819179/1587138

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!