Android学习笔记之viewholder

在adapter中通过使用静态内部类(viewholder)缓存组件的引用来防止ListView刷新时重新LayoutInflater跟findViewById从而达到优化的目的。示例如下:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.menu_item_layout, null);
viewHolder = new ViewHolder();
viewHolder.btn_menu = (Button) convertView.findViewById(R.id.btn_menu);
viewHolder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
final int[] d= data.get(position);
viewHolder.btn_menu.setBackgroundResource(d[0]);
viewHolder.btn_menu.setFocusable(false);
viewHolder.btn_menu.setFocusableInTouchMode(false);
viewHolder.tv_title.setText(d[1]);
viewHolder.tv_title.setTextSize(12);
viewHolder.tv_title.setTextColor(Color.WHITE);
final View t_View = convertView;
viewHolder.btn_menu.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if(mItemListener != null) {
mItemListener.onClickListener(position, t_View);
}
}
}); return convertView;
} public static class ViewHolder {
public Button btn_menu;
public TextView tv_title;
}
上一篇:帝国cms修改栏目后文章列表的url错误怎么解决


下一篇:自动化测试基础篇--Selenium中JS处理浏览器弹窗