标签:listview优化
package com.example.ex_000_example;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
//准备好的图片资源
private int[] imageList = new int[] { R.drawable.qq0, R.drawable.qq1,
R.drawable.qq2, R.drawable.qq3, R.drawable.qq4, R.drawable.qq5,
R.drawable.qq6, R.drawable.qq7, R.drawable.qq8, R.drawable.qq9,
R.drawable.qq10, R.drawable.qq11, R.drawable.qq12, R.drawable.qq13,
R.drawable.qq14 };
private ArrayList<MyDate> dateList = new ArrayList<MyDate>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 准备数据
for (int i = 0; i < imageList.length; i++) {
MyDate myDate = new MyDate("数据来了"+i, imageList[i]);
dateList.add(myDate);
}
ListView listView = (ListView) findViewById(R.id.listView1);
MyAdapter myAdapter = new MyAdapter();
// 设置适配器
listView.setAdapter(myAdapter);
}
//减少控件查找次数
class ViewHolder {
TextView textView;
ImageView imageView;
}
class MyAdapter extends BaseAdapter {
// 设定每行的样式以及显示
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View inflate = null;
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
// 得到布局转换器
LayoutInflater inflater = getLayoutInflater();
// 将布局xml文件转换成布局对象
inflate = inflater.inflate(R.layout.list_item, null);
// 查找控件,一定要查找该布局对象上有的控件
holder.textView = (TextView) inflate
.findViewById(R.id.textView1);
holder.imageView = (ImageView) inflate
.findViewById(R.id.imageView1);
// 将保存控件的对象存入行布局对象的tag中
inflate.setTag(holder);
} else {
// 让那个已经完全消失的布局对象,去替代即将出现的
inflate = convertView;
// 从行布局对象中取出之前存好的控件
holder = (ViewHolder) inflate.getTag();
}
// 根据position从容器中取出数据
MyDate myDate = dateList.get(position);
// 将数据显示到控件上
holder.textView.setText(myDate.title);
holder.imageView.setImageResource(myDate.image);
return inflate;
}
// 决定了listview显示的行数
@Override
public int getCount() {
// TODO Auto-generated method stub
return dateList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyDate{
String title;
int image;
public MyDate(String title, int image) {
super();
this.title = title;
this.image = image;
}
}
}
本文出自 “破公子” 博客,请务必保留此出处http://pogongzi.blog.51cto.com/8982426/1621265
标签:listview优化
原文地址:http://pogongzi.blog.51cto.com/8982426/1621265