Android ListView中设置的字体颜色显示错乱问题 && SparseArray<E>的使用

刚解决完ListView中的CheckBox选项位置错乱的问题,又出现了ListView中设置的字体颜色位置错乱的问题抓狂

先贴修改之前的代码(Adapter中的getView()方法):

        @Override
	public View getView(int position, View convertView, ViewGroup parent) {

		MultiSelLvOneViewHolder holder;
		if (convertView == null) {
			// 获得ViewHolder对象
			holder = new MultiSelLvOneViewHolder();
			// 导入布局并赋值
			convertView = mInflater.inflate(
					R.layout.acat_listitem_multi_select_level_one, null);
			holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
			holder.tvId = (TextView) convertView.findViewById(R.id.tvId);
			holder.ivRightArrow = (ImageView) convertView.findViewById(R.id.ivRightArrow);
			// 设置标签
			convertView.setTag(holder);
		} else {
			// 取出ViewHolder
			holder = (MultiSelLvOneViewHolder) convertView.getTag();
		}

		// 设置列表的显示信息
		Resources resource = (Resources) context.getResources();
		holder.tvInfo.setText(list.get(position)
				.get(CommonConstants.MULTI_SELECT_LIST_KEY_TITLE).toString());
		String id = list.get(position).get(CommonConstants.MULTI_SELECT_LIST_KEY_INFO)
				.toString();
		holder.tvId.setText(id);
		// 判断职业是否为总类别,总类别时隐藏右箭头,字体颜色变为橙色
		if (JobTypeUtils.isTotalJobType(id)) {
			holder.ivRightArrow.setVisibility(View.GONE);
			holder.tvInfo.setTextColor(resource.getColorStateList(R.color.deep_yellow));
		} else {
			holder.ivRightArrow.setVisibility(View.VISIBLE);
			holder.tvId.setTextColor(resource.getColorStateList(R.color.black));
		}

		return convertView;
	}
加粗部分为设置字体的颜色。。可是为神马不好使呢大哭

参考了各种资料,何种修改可惜都没用。。

最后参考了boylinux的文章,地址:http://blog.csdn.net/boylinux/article/details/8860443

添加一个SparseArray<View>用于缓存已经显示过的View,代码修改如下:

public class AcatMultiSelectLevelOneAdapter extends BaseAdapter {

	/* 稀疏数组:用于缓存已显示过的View */
	private SparseArray<View> viewArray = null;
	/* 填充数据列表 */
	private List<Map<String, Object>> list;
	/* 用来导入布局 */
	private LayoutInflater mInflater = null;
	/* 字体颜色 */
	private ColorStateList col1 = null;
	private ColorStateList col2 = null;

	// 构造器
	public AcatMultiSelectLevelOneAdapter(Context context, List<Map<String, Object>> list) {
		this.list = list;
		mInflater = LayoutInflater.from(context);
		col1 = context.getResources().getColorStateList(R.color.deep_yellow);
		col2 = context.getResources().getColorStateList(R.color.black);
		viewArray = new SparseArray<View>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		MultiSelLvOneViewHolder holder;
		if (viewArray.get(position, null) == null) {

			holder = new MultiSelLvOneViewHolder();
			// 导入布局控件
			convertView = mInflater.inflate(
					R.layout.acat_listitem_multi_select_level_one, null);
			holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
			holder.tvId = (TextView) convertView.findViewById(R.id.tvId);
			holder.ivRightArrow = (ImageView) convertView.findViewById(R.id.ivRightArrow);
			// 为控件赋值
			holder.tvInfo.setText(list.get(position)
					.get(CommonConstants.MULTI_SELECT_LIST_KEY_TITLE).toString());
			String id = list.get(position)
					.get(CommonConstants.MULTI_SELECT_LIST_KEY_INFO).toString();
			holder.tvId.setText(id);
			// 判断职业是否为总类别,总类别时隐藏右箭头,字体颜色变为橙色
			if (JobTypeUtils.isTotalJobType(id)) {
				holder.ivRightArrow.setVisibility(View.GONE);
				holder.tvInfo.setTextColor(col1);
			} else {
				holder.ivRightArrow.setVisibility(View.VISIBLE);
				holder.tvId.setTextColor(col2);
			}
			// 设置标签
			convertView.setTag(holder);
			// 保存View到缓存Map中
			viewArray.put(position, convertView);
		} else {
			// 从缓存Map取出View
			convertView = viewArray.get(position);
			// 取出标签的ViewHolder
			holder = (MultiSelLvOneViewHolder) convertView.getTag();
		}

		return convertView;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}


根据上述修改后,初次滑动ListView时都会新建一个View,然后把它放到缓存的SparseArray<View>中,虽然开始时会消耗一些内存,

但再次滑动显示时就可以拿到同一个的View实例,最最重要的是....显示的字体颜色不再错乱了!!!再次感谢boylinux~~


另外,关于SparseArray<E>的使用,我就直接复制别人的帖子了。。

原创帖子:http://liuzhichao.com/p/832.html

修改数据方法:

public void put(int key, E value)
public void setValueAt(int index, E value)

查找数据方法:

public E get(int key)
public E get(int key, E valueIfKeyNotFound)
其中get(int key)也只是调用了 get(int key,E valueIfKeyNotFound),最后一个从传参的变量名就能看出,传入的是找不到的时候返回的值.get(int key)当找不到的时候,默认返回null。

其他方法就参考原帖吧。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值