最近在项目遇到一个问题,就是在ExpandListView中有多个ItemView和groupView 每次都需要初始化,这个效率只能呵呵了!思路是如此出发的!
以前我们使用在一个Listview只有单个itemView优化如下!
FitListViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.fitlistitem, null); viewHolder = new FitListViewHolder(convertView, context); convertView.setTag(viewHolder); } else { viewHolder = (FitListViewHolder) convertView.getTag(); } // 装载数据 viewHolder.setContent(yiLists.get(position));
大部分人应该都是这么处理的,我也是这样处理的!
一次类推如果为多个责直接扩展为:
if (symptomResultType == SymptomResultType.SYMPTOM_TYPE_COMMON_METHOD) { CommonMethodViewHolder viewHolder; if(convertView == null || !(convertView.getTag() instanceof CommonMethodViewHolder)) { convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_common_method, null); viewHolder = new CommonMethodViewHolder(convertView, mContext); convertView.setTag(viewHolder); }else{ viewHolder = (CommonMethodViewHolder) convertView.getTag(); } Object[] userMethods = userSymptomResultModel.userMethodDatas.toArray(); viewHolder.setMethodContent((UserMethod) userMethods[childPosition]); return convertView; } else if (symptomResultType == SymptomResultType.SYMPTOM_TYPE_HABIT) { UserHabitViewHolder viewHolder; if(convertView == null || !(convertView.getTag() instanceof UserHabitViewHolder)) { convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_habit, null); viewHolder = new UserHabitViewHolder(convertView, mContext); convertView.setTag(viewHolder); }else{ viewHolder = (UserHabitViewHolder) convertView.getTag(); } Object[] userHabits = userSymptomResultModel.userHabitDatas.toArray(); viewHolder.setHabitContent((UserHabit) userHabits[childPosition]); return convertView; }
中心思想就是根据contentView是否为空或者不为空的情况下获取gettag然后看看是否是当前item需要的View,如果不是责再去创建。根据tag去获取就OK了!
debug执行了我想要的结果!
欢迎大家拍砖!
时间: 2024-10-13 08:57:23