[java]代码库package com.example.day10_listview;
import java.util.List;
import com.example.day10_listview.bean.Info;
import com.lidroid.xutils.BitmapUtils;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private List list;
public MyBaseAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 得到判断好的视图类型
int type = getItemViewType(position);
ViewHolder3 holder3 = null;
ViewHolder2 holder2 = null;
ViewHolder1 holder1 = null;
if (convertView == null) {
// 通过视图类型填充不同的布局
switch (type) {
case 0:
convertView = View.inflate(context, R.layout.item2, null);
holder2 = new ViewHolder2();
holder2.imageView = (ImageView) convertView
.findViewById(R.id.item2_imageView);
convertView.setTag(holder2);
break;
case 1:
convertView = View.inflate(context, R.layout.item1, null);
holder1 = new ViewHolder1();
holder1.textView = (TextView) convertView
.findViewById(R.id.item1_textView);
convertView.setTag(holder1);
break;
case 2:
convertView = View.inflate(context, R.layout.item3, null);
holder3 = new ViewHolder3();
holder3.textView = (TextView) convertView
.findViewById(R.id.item3_textView);
holder3.imageView = (ImageView) convertView
.findViewById(R.id.item3_imageView);
convertView.setTag(holder3);
break;
}
} else {
switch (type) {
case 0:
holder2 = (ViewHolder2) convertView.getTag();
break;
case 1:
holder1 = (ViewHolder1) convertView.getTag();
break;
case 2:
holder3 = (ViewHolder3) convertView.getTag();
break;
}
}
///以上是listView优化,以下是赋值/
BitmapUtils bitmapUtils=new BitmapUtils(context);
Info info = list.get(position);
switch (type) {
case 0:
bitmapUtils.display(holder2.imageView, info.firstImg);
break;
case 1:
holder1.textView.setText(info.title);
break;
case 2:
holder3.textView.setText(info.title);
bitmapUtils.display(holder3.imageView, info.firstImg);
break;
}
return convertView;
}
@Override
// 条目视图的类型
public int getItemViewType(int position) {
Info info = list.get(position);
String firstImg = info.firstImg;
String title = info.title;
// 有图片的地址,但是没有文字
if (!TextUtils.isEmpty(firstImg) && TextUtils.isEmpty(title)) {
return 0;
} else if (TextUtils.isEmpty(firstImg) && !TextUtils.isEmpty(title)) {
// 没有图片的地址,但是有文字
return 1;
} else if (!TextUtils.isEmpty(firstImg) && !TextUtils.isEmpty(title)) {
// 有图片的地址,也有文字
return 2;
}
return 1;
}
@Override
// 条目类型总数
public int getViewTypeCount() {
return 3;
}
class ViewHolder3 {
TextView textView;
ImageView imageView;
}
class ViewHolder1 {
TextView textView;
}
class ViewHolder2 {
ImageView imageView;
}
}