- //listView显示不同条目的Adapter,比平常用的多了两个方法
- class MyAdapter extends BaseAdapter{
- @Override
- public int getCount() {
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- //表示有几种类型的item,这里有三种
- @Override
- public int getViewTypeCount() {
- return 4;//这里注意必须比上面定义的数要大
- }
- //返回ListView所加载的item的类型
- @Override
- public int getItemViewType(int position) {
- return list.get(position).type;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder1 holder1=null;
- ViewHolder2 holder2=null;
- ViewHolder3 holder3=null;
- int type = getItemViewType(position);
- if (convertView==null){
- //存值
- switch (type){
- case FristType:
- holder1=new ViewHolder1();
- convertView=View.inflate(MainActivity.this,R.layout.item,null);
- holder1.tv1=(TextView) convertView.findViewById(R.id.tv1);
- convertView.setTag(holder1);
- break;
- case TwoType:
- holder2=new ViewHolder2();
- convertView=View.inflate(MainActivity.this,R.layout.item2,null);
- holder2.tv2=(TextView) convertView.findViewById(R.id.tv2);
- holder2.iv2=(ImageView)convertView.findViewById(R.id.iv2);
- convertView.setTag(holder2);
- break;
- case ThreeType:
- holder3=new ViewHolder3();
- convertView=View.inflate(MainActivity.this,R.layout.item3,null);
- holder3.tv3=(TextView) convertView.findViewById(R.id.tv3);
- holder3.iv3=(ImageView)convertView.findViewById(R.id.iv3);
- convertView.setTag(holder3);
- break;
- }
- }else {
- //取值
- switch (type){
- case FristType:
- holder1=(ViewHolder1) convertView.getTag();
- break;
- case TwoType:
- holder2=(ViewHolder2) convertView.getTag();
- break;
- case ThreeType:
- holder3=(ViewHolder3) convertView.getTag();
- break;
- }
- }
- //设置值
- switch (type){
- case FristType:
- holder1.tv1.setText(list.get(position).text);
- break;
- case TwoType:
- holder2.tv2.setText(list.get(position).text);
- holder2.iv2.setImageResource(list.get(position).images);
- break;
- case ThreeType:
- holder3.tv3.setText(list.get(position).text);
- holder3.iv3.setImageResource(list.get(position).images);
- break;
- }
- return convertView;
- }
- class ViewHolder1{
- TextView tv1;
- }
- class ViewHolder2{
- TextView tv2;
- ImageView iv2;
- }
- class ViewHolder3{
- TextView tv3;
- ImageView iv3;
- }
- }
- }
11-25