ListView使用item显示不同布局

   /**
* 自定义城市列表适配器
*/
private class MyCityListAdapter extends BaseAdapter { final int VIEW_TYPE = 2;
final int TYPE_1 = 0;
final int TYPE_2 = 1; @Override
public int getCount() {
return cityList.size();
} @Override
public Object getItem(int position) {
return cityList.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public int getItemViewType(int position) { if (position == 3||position == 5||position == 10)
return TYPE_1;
else
return TYPE_2;
} @Override
public int getViewTypeCount() {
return VIEW_TYPE;
} ViewHolder viewHolder; @Override
public View getView(int position, View convertView, ViewGroup parent) { viewHolder = new ViewHolder();
if (convertView == null) { //对listView设置不同布局
int type = getItemViewType(position);
Log.e("打印值", "getView " + position + " " + convertView + " type = " + type); switch (type) {
case TYPE_1:
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.city_view_item_sort, null);
break;
case TYPE_2:
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.city_view_item, null);
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.textView = (TextView) convertView.findViewById(R.id.cityName);
viewHolder.textView.setText(cityList.get(position).getCityName());
return convertView;
}
} private class ViewHolder {
private TextView textView;
}
上一篇:thinkphp 模型、控制器、视图


下一篇:微信小程序demo2