一般adapter的做法会重写getView方法
比如
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contentitem, null);
}
TextView title = (TextView) convertView.findViewById(R.id.textViewTitle);
TextView author = (TextView) convertView.findViewById(R.id.textViewAuthor);
TextView content = (TextView) convertView.findViewById(R.id.textViewContent);
TextView otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
ImageView contentImage = (ImageView)convertView.findViewById(R.id.imageView);
ContentInfo info = data.get(position);
title.setText(info.title);
author.setText(info.author);
content.setText(info.content);
otherInfo.setText(info.otherInfo);
new HttpImageLoader(contentImage).load(info.imageUri);
convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
return convertView;
}
这样写有一个问题,就是如果我的图片比较大,contentImage 的加载时间就会比较长,那么当你很快的滚动listview的时候,就会刷新不过来。
为此我做了这样一个缓存
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contentitem, null);
} TextView title = (TextView) convertView.findViewById(R.id.textViewTitle);
TextView author = (TextView) convertView.findViewById(R.id.textViewAuthor);
TextView content = (TextView) convertView.findViewById(R.id.textViewContent);
TextView otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
ImageView contentImage = (ImageView)convertView.findViewById(R.id.imageView);
ContentInfo info = data.get(position);
title.setText(info.title);
author.setText(info.author);
content.setText(info.content);
otherInfo.setText(info.otherInfo);
new HttpImageLoader(contentImage).load(info.imageUri);
convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)); return convertView;
} private class HttpImageLoader{
private Bitmap bitmap;
private ImageView image; final android.os.Handler handler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
image.setImageBitmap(bitmap);
}
}; public HttpImageLoader(ImageView view){
image = view;
}
public void load(String url){
final String u = url;
if (map.containsKey(url)){
image.setImageBitmap(map.get(url));
return;
}
new Thread() {
@Override
public void run() {
bitmap = HttpUtil.getHttpBitmap(u);
map.put(u,bitmap);
handler.sendEmptyMessage(0);
}
}.start(); }
}
HttpImageLoader类中,每次加载一个图片就会将这个图片缓存起来放入到map中,这样省去了重新从网络读取的时间。完全是从本地加载。
效果比之前好很多,但是还是会卡。
最后采用了最土的方法。
添加的时候,直接new一个view出来,然后将整个view放入到缓存中。
public void add(final ContentInfo info) {
ContentItemView contentItemView = new ContentItemView(context);
contentItemView.setContentInfo(info);
contentItemView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)); contentItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,ArticleActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("info",info);
context.startActivity(intent);
}
});
data.add(contentItemView);
}
getView的时候直接从代码中将整个view取出来
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return data.get(position);
}
这样虽然比较耗内存,但是整个会变得很流畅。
不过如果这样做的话,还不如直接用Scrollview+linearLayout的组合比较合适。
当然了,前提是我能够保证在listview中的item不会太多,内存的消耗能够在我的容忍范围之内,才可以这样做。