前言:
android开发ListView肯定是再常用不过了,用Baseadapter出现了item中指定item颜色混乱问题,仔细思考外加网上查阅资料,总结为item复用问题造成的。
屏幕外隐藏的item会复用之前的item,就会造成了颜色混乱。
上图红色字体就会和其他item灰色字体混乱。
出现混乱的代码:(代码可以运行,只是item颜色混乱)
class MyAdapter extends BaseAdapter {
private BitmapUtils mBitmapUtils;
private List<ListBean> list;
private ViewHolder holder;
private SparseArray<View> viewArray = null;
public MyAdapter(List<ListBean> list) {
this.list = list;
mBitmapUtils = new BitmapUtils(context);
mBitmapUtils.configDefaultLoadingImage(R.drawable.default_img);// 设置默认加载图片
viewArray = new SparseArray<View>();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_weixinchoice,
null);
holder = new ViewHolder();
holder.ig_firstImg = (ImageView) convertView
.findViewById(R.id.ig_firstImg);
holder.ig_firstImg.setScaleType(ScaleType.FIT_XY);// 设置图片缩放模式
// im_1.setScaleType(ScaleType.FIT_CENTER);
holder.tv_title = (TextView) convertView
.findViewById(R.id.tv_title);
holder.tv_source_or_mark = (TextView) convertView
.findViewById(R.id.tv_source_or_mark);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String imageUrl = mList.get(position).getFirstImg();// 获取图片的url
if (!TextUtils.isEmpty(mList.get(position).getMark())) {
// 设置字体颜色
holder.tv_source_or_mark.setTextColor(Color.RED);
holder.tv_source_or_mark.setText(mList.get(position).getMark());
}
else {
holder.tv_source_or_mark.setTextColor(Color.GRAY);
holder.tv_source_or_mark.setText(mList.get(position)
.getSource());
}
ListBean listBean = list.get(position);
// 查看本地是否存有read_ids,根据是否有,设置字体颜色
String read_ids = PrefUtils.getString(context, "read_ids", "");
if (!TextUtils.isEmpty(read_ids)) {
if (read_ids.contains(listBean.getId() + "")) {
holder.tv_title.setTextColor(Color.GRAY);
} else {
holder.tv_title.setTextColor(Color.BLACK);
}
holder.tv_title.setText(mList.get(position).getTitle());
}
// 下载图片-将图片设置给imageview-避免内存溢出-缓存
// BitmapUtils-XUtils
mBitmapUtils.display(holder.ig_firstImg, listBean.getFirstImg());
return convertView;// 返回当前行的视图,然后把视图放到listview(父容器)
}
}
解决办法:
添加一个SparseArray<View>用于缓存已经显示过的View,代码修改如下:
class MyAdapter extends BaseAdapter {
private BitmapUtils mBitmapUtils;
private List<ListBean> list;
private ViewHolder holder;
/*
* 稀疏数组:用于缓存已显示过的View
*
* 防止出现item中颜色混乱现象
*/
private SparseArray<View> viewArray = null;
public MyAdapter(List<ListBean> list) {
this.list = list;
mBitmapUtils = new BitmapUtils(context);
mBitmapUtils.configDefaultLoadingImage(R.drawable.default_img);// 设置默认加载图片
viewArray = new SparseArray<View>();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/*此getView()方法解决了item中view颜色混乱的问题
*
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (viewArray.get(position, null) == null) {
convertView = View.inflate(context, R.layout.item_weixinchoice,
null);
holder = new ViewHolder();
holder.ig_firstImg = (ImageView) convertView
.findViewById(R.id.ig_firstImg);
holder.ig_firstImg.setScaleType(ScaleType.FIT_XY);// 设置图片缩放模式
// im_1.setScaleType(ScaleType.FIT_CENTER);
holder.tv_title = (TextView) convertView
.findViewById(R.id.tv_title);
holder.tv_source_or_mark = (TextView) convertView
.findViewById(R.id.tv_source_or_mark);
String imageUrl = mList.get(position).getFirstImg();// 获取图片的url
if (!TextUtils.isEmpty(mList.get(position).getMark())) {
// 设置字体颜色
holder.tv_source_or_mark.setTextColor(Color.RED);
holder.tv_source_or_mark.setText(mList.get(position)
.getMark());
} else {
holder.tv_source_or_mark.setTextColor(Color.GRAY);
holder.tv_source_or_mark.setText(mList.get(position)
.getSource());
}
ListBean listBean = list.get(position);
// 查看本地是否存有read_ids,根据是否有,设置字体颜色
String read_ids = PrefUtils.getString(context, "read_ids", "");
if (!TextUtils.isEmpty(read_ids)) {
if (read_ids.contains(listBean.getId() + "")) {
holder.tv_title.setTextColor(Color.GRAY);
} else {
holder.tv_title.setTextColor(Color.BLACK);
}
holder.tv_title.setText(mList.get(position).getTitle());
}
// 下载图片-将图片设置给imageview-避免内存溢出-缓存
// BitmapUtils-XUtils
mBitmapUtils
.display(holder.ig_firstImg, listBean.getFirstImg());
// 设置标签
convertView.setTag(holder);
// 保存View到缓存Map中
viewArray.put(position, convertView);
} else {
// 从缓存Map取出View
convertView = viewArray.get(position);
// 取出标签的ViewHolder
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
参考博客:http://blog.csdn.net/kakugyouko/article/details/49866689