}
-------------------------ListView之BaseAdapter-----------------------------------------------
public class TestAdapter extends BaseAdapter {
private Context mContext;
private List<TestBean> mDatas;
private LayoutInflater mInflater;
private long time;
public TestAdapter(Context context, List<TestBean> mDatas) {
this.mContext = context;
this.mDatas = mDatas;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {// 获取到适配器中数据的总(条)数
// TODO Auto-generated method stub
return mDatas.size();
}
@Override
public Object getItem(int position) {// 获取数据中指定索引对应的数据项
// TODO Auto-generated method stub
return mDatas.get(position);
}
@Override
public long getItemId(int position) {// 获取指定行的id
// TODO Auto-generated method stub
return position;
}
/**
* 建议作用的通用方法:
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {// 显示出每一个Item的具体内容
long start = System.currentTimeMillis();
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();// 如果不用HolderView机制,直接在这里面新建各种View,那完全没有进行优化,性能非常低
convertView = mInflater.inflate(R.layout.lv_item, null);
holder.itemIv = (ImageView) convertView.findViewById(R.id.item_iv);
holder.itemTitle = (TextView) convertView.findViewById(R.id.item_title);
holder.itemContent = (TextView) convertView.findViewById(R.id.item_content);
convertView.setTag(holder);// 通过setTag()把ViewHolder与convertView关联
}
else {
holder = (ViewHolder) convertView.getTag();// 当convertView不为null时,我们直接用getTag()取出关联的ViewHolder
}
// 在这里为Item设置具体的数据
TestBean bean = mDatas.get(position);
holder.itemIv.setImageResource(bean.getImageId());
holder.itemTitle.setText(bean.getTitle());
holder.itemContent.setText(bean.getContent());
long end = System.currentTimeMillis();
time += end - start;
Log.e("ldm_test", time + "");// 我们可能分别对三种方式进行时间差计算,可以明显看出三种方式的性能优势
return convertView;
}
----------------第一境界:"傻瓜"式的写法,性能最差,建议不使用------------------------------------------------------------
// @Override
// public View getView(int position, View convertView, ViewGroup parent) {// 显示出每一个Item的具体内容
// View view = mInflater.inflate(R.layout.lv_item, null);
// ImageView itemIv = (ImageView) view.findViewById(R.id.item_iv);
// TextView itemTitle = (TextView) view.findViewById(R.id.item_title);
// TextView itemContent = (TextView) view.findViewById(R.id.item_content);
//
// // 在这里为Item设置具体的数据
// TestBean bean = mDatas.get(position);
// itemIv.setImageResource(bean.getImageId());
// itemTitle.setText(bean.getTitle());
// itemContent.setText(bean.getContent());
// return view;
// }
----------------第二境界:普通写法,也用到convertView作缓存,但不建议使用------------------------------------------
// @Override
// public View getView(int position, View convertView, ViewGroup parent) {// 显示出每一个Item的具体内容
// if (convertView == null) {//通过简单的判断,就避免创建大量的convertView对象,提升了性能
// convertView = mInflater.inflate(R.layout.lv_item, null);
// }
// //下面的三句话同样会不断地findViewById(),浪费性能
// ImageView itemIv = (ImageView) convertView.findViewById(R.id.item_iv);
// TextView itemTitle = (TextView) convertView.findViewById(R.id.item_title);
// TextView itemContent = (TextView) convertView.findViewById(R.id.item_content);
//
// // 在这里为Item设置具体的数据
// TestBean bean = mDatas.get(position);
// itemIv.setImageResource(bean.getImageId());
// itemTitle.setText(bean.getTitle());
// itemContent.setText(bean.getContent());
// return convertView;
// }
---------------每三境界:通常我们就是用ViewHolder来利用ListView的缓存,提升性能必须的-----------------------------------
class ViewHolder {
ImageView itemIv;
TextView itemTitle;
TextView itemContent;
}
}
--------------实体Bean-----------------------------
public class TestBean {
private int imageId;// 图片id
private String title;// 标题
private String content;// 内容
public TestBean(int imageId, String title, String content) {
this.imageId = imageId;
this.title = title;
this.content = content;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
------------Item布局文件lv_item,Activity的布局文件就一ListView------------------------------------------------------
<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="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/item_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/item_iv"
android:textSize="18sp" />
<TextView
android:id="@+id/item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_below="@id/item_title"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/item_iv"
android:textSize="15sp" />
</RelativeLayout>