效果图
步骤
- 定义item,布局文件中引入ListView标签
- 定义一个bean类,用于对item中数据进行包装
- 定义adapter,继承baseAdapter
- activity中,定义一个List,将bean数据初始化,并add到Lsit中;List传给adapter,adapter和listview绑定。
区别就在于adapter的getView方法中,分为三种。
1、直接定义一个view,然后返回view
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflator.inflate(R.layout.item, null);
ImageView image = (ImageView) view.findViewById(R.id.image);
TextView title = (TextView) view.findViewById(R.id.title);
TextView content = (TextView) view.findViewById(R.id.content);
ItemBean itemBean = itemBeanList.get(position);
image.setImageResource(itemBean.ItemImageId);
title.setText(itemBean.ItemTitle);
content.setText(itemBean.ItemContent);
return view;
}
这种方法简单粗暴。每次都是重新inflate一个view,却没有充分利用好ListView的回收机制。用于简单布局时还好,但对于复杂布局时性能就会大大下降。方法2就充分利用了这一点。
2、先判断convertView为空时才重新inflate
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = inflator.inflate(R.layout.item, null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView content = (TextView) convertView.findViewById(R.id.content);
ItemBean itemBean = itemBeanList.get(position);
image.setImageResource(itemBean.ItemImageId);
title.setText(itemBean.ItemTitle);
content.setText(itemBean.ItemContent);
return convertView;
}
仔细观察会发现,每次getView都会重新findViewById。这同样会降低性能。此时可利用一个ViewHolder对find的组件进行存储。
3、先定义一个ViewHolder,用于存储组件。convertView为空时,就inflate并find组件,并将find的组件用setTag存放到ViewHolder中。不为空时,getTag方法取出ViewHolder。
class ViewHolder {
public ImageView image;
public TextView title;
public TextView content;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = inflator.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.content = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
ItemBean itemBean = itemBeanList.get(position);
viewHolder.image.setImageResource(itemBean.ItemImageId);
viewHolder.title.setText(itemBean.ItemTitle);
viewHolder.content.setText(itemBean.ItemContent);
return convertView;
}
不怕浪费时间的话,可以获取开始加载和加载完成时的时间差,就会发现方法3性能时最好的。因此开发时推荐用该方法。
具体源码
1、item的布局item.xml
2、ItemBean,用于对item中组件进行包装
public class ItemBean {
public int ItemImageId;
public String ItemTitle;
public String ItemContent;
public ItemBean(int itemImageId, String itemTitle, String itemContent) {
super();
ItemImageId = itemImageId;
ItemTitle = itemTitle;
ItemContent = itemContent;
}
}
3、MainActivity
public class MainActivity extends Activity{
private ListView listView;
private List itemBeanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv);
itemBeanList = new ArrayList();
for(int i=0;i<10;i++){
itemBeanList.add(new ItemBean(
R.drawable.ic_launcher,
"标题"+(i+1),
"内容"+(i+1)));
}
listView.setAdapter(new ListViewAdapter(itemBeanList,this));
}
}
4、ListViewAdapter
public class ListViewAdapter extends BaseAdapter{
private List itemBeanList;
private Context context;
private LayoutInflater inflator;
public ListViewAdapter(List itemBeanList,Context context) {
super();
this.itemBeanList = itemBeanList;
this.context = context;
inflator = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemBeanList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return itemBeanList.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = inflator.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.content = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
ItemBean itemBean = itemBeanList.get(position);
viewHolder.image.setImageResource(itemBean.ItemImageId);
viewHolder.title.setText(itemBean.ItemTitle);
viewHolder.content.setText(itemBean.ItemContent);
return convertView;
}
class ViewHolder {
public ImageView image;
public TextView title;
public TextView content;
}
}