如果ListView不懂,请绕路
1.ListView添加多个item必须用到的两个方法
getViewTypeCount一共有多少种item,我这里写的两种 getItemViewType当前position用到的是那种类型 注意viewType返回的值只能小于getViewTypeCount,不然运行时一直提示转换类型错误,或者数组越界等莫名其妙的错误
@Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if (position == 0) { return 0; } return 1; }
2.重写getView方法
@Override public View getView(final int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); ViewHolderTotalCount totalCount; ViewHolder holder; if (convertView == null) { switch (type) { case 0: { convertView = View.inflate(context, R.layout.item_total_count, null); totalCount = new ViewHolderTotalCount();//下面对hoder的子空间赋值 convertView.setTag(totalCount); } break; default: { convertView = View.inflate(context, R.layout.item_customer_pool, null); holder = new ViewHolder();//下面对hoder的子空间赋值 convertView.setTag(holder); } break; } } switch (type) { case 0: totalCount = (ViewHolderTotalCount) convertView.getTag(); //这里对holder 的TextView等空间赋值 break; default: holder = (ViewHolder) convertView.getTag(); // //这里对holder 的TextView等空间赋值 break; } return convertView; }
如果这两个你设置了,还有错误,可以把代码截图给我,欢迎提问,