最近在项目遇到一个问题,就是在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; }
debug执行了我想要的结果!
欢迎大家拍砖!
ExpandListView 多个ItemView的效率优化
原文地址:http://blog.csdn.net/zhangxiweicaochen/article/details/40922133