在自定义adapter中需要重写两个方法:getItemViewType() 、 getViewTypeCount(), getViewTypeCount这个方法返回是布局的数量,当返回的布局大于1时,adapter 会分配多个convertview,而根据getItemViewType 返回的type进行标记, getItemViewType返回的type是从0-n 开始的,也就是和数组的下标相同,如果返回的type >=getViewTypeCount
就会报错。以下代码是业务逻辑的一部分。仅供参考。
public class MessageFlowAdapter extends BaseAdapter {
Context mContext;
private LayoutInflater mInflater;
private List<MessageFlowModel> messageList;
private Context context;
private DisplayImageOptions options;
private final static float imgWidth = 400f;
private int width;
private final static int TYPE_1 = 0;
private final static int TYPE_2 = 1;
private final static int TYPE_3 = 2;
private final static int TYPE_4 = 3;
private int textViewWidth = 0;
private int alphaCardId[] = {R.drawable.messagebox_car1, R.drawable.messagebox_car2, R.drawable.messagebox_car3, R.drawable.messagebox_car4};
private long currentTime;
private int timePosition;
private boolean timeSetFlag = false;
public MessageFlowAdapter(Activity context, List<MessageFlowModel> messageList, DisplayImageOptions options, long currentTime, int timePosition) {
this.messageList = messageList;
this.context = context;
this.options = options;
Display display = context.getWindowManager().getDefaultDisplay();
System.out.println(" width Message screen : " + display.getWidth());
width = (display.getWidth() * 4 / 6) - 50;
mInflater = LayoutInflater.from(context);
this.currentTime = currentTime;
this.timePosition = timePosition;
}
public boolean isTimeSetFlag() {
return timeSetFlag;
}
public void setTimeSetFlag(boolean timeSetFlag) {
this.timeSetFlag = timeSetFlag;
}
public int getTimePosition() {
return timePosition;
}
public void setTimePosition(int timePosition) {
this.timePosition = timePosition;
}
public void setData(List<MessageFlowModel> messageList) {
this.messageList = messageList;
}
public final class ViewHolderConsultantNoCar {
public TextView consultantWords;
public TextView timeLong;
}
public final class ViewHolderConsultantWords {
public TextView consultantWords;
public TextView timeLong;
}
public final class ViewHolderCustomWords {
public TextView customWords;
public TextView timeLong;
}
public final class ViewHolderRecommendCar {
public TextView recommendWords;
public ImageView alphaImage;
public ImageView carImage;
public RelativeLayout recommendCarRelative;
public TextView timeLong;
public TextView timeShort;
public TextView carNum;
public ImageView carShadow;
public TextView timeCount;
}
@Override
public int getCount() {
return messageList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderConsultantWords holderConsultantWords = null;
ViewHolderCustomWords holderCustomWords = null;
ViewHolderRecommendCar holderRecommendCar = null;
ViewHolderConsultantNoCar holderConsultantNoCar = null;
final int pos = position;
//判断是哪种消息 0为欢迎 1为推荐车辆 2为客户设置筛选
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_1:
convertView = mInflater.inflate(R.layout.consultant_words, null);
holderConsultantWords = new ViewHolderConsultantWords();
holderConsultantWords.consultantWords = (TextView) convertView.findViewById(R.id.consultant_words);
holderConsultantWords.timeLong = (TextView) convertView.findViewById(R.id.consultant_words_time);
convertView.setTag(R.layout.consultant_words, holderConsultantWords);
break;//顾问消息
case TYPE_2:
convertView = mInflater.inflate(R.layout.consultant_recommend, null);
holderRecommendCar = new ViewHolderRecommendCar();
holderRecommendCar.carImage = (ImageView) convertView.findViewById(R.id.recommend_car_img);
// holderRecommendCar.alphaImage = (ImageView) convertView.findViewById(R.id.alpha_cards);
holderRecommendCar.recommendCarRelative = (RelativeLayout) convertView.findViewById(R.id.recommend_car_relative);
holderRecommendCar.recommendWords = (TextView) convertView.findViewById(R.id.recommend_car_text);
holderRecommendCar.timeLong = (TextView) convertView.findViewById(R.id.recommend_cars_time);
holderRecommendCar.timeShort = (TextView) convertView.findViewById(R.id.message_time);
holderRecommendCar.carNum = (TextView) convertView.findViewById(R.id.message_car_totalnum);
holderRecommendCar.carShadow = (ImageView) convertView.findViewById(R.id.car_image_shadow);
holderRecommendCar.timeCount = (TextView) convertView.findViewById(R.id.recommend_time_count);
convertView.setTag(R.layout.consultant_recommend, holderRecommendCar);
break;//推荐车辆
case TYPE_3:
convertView = mInflater.inflate(R.layout.customer_words, null);
holderCustomWords = new ViewHolderCustomWords();
holderCustomWords.customWords = (TextView) convertView.findViewById(R.id.customer_words);
holderCustomWords.timeLong = (TextView) convertView.findViewById(R.id.customer_words_time);
convertView.setTag(R.layout.customer_words, holderCustomWords);
break; //客户消息
case TYPE_4:
convertView = mInflater.inflate(R.layout.consultant_nocar, null);
holderConsultantNoCar = new ViewHolderConsultantNoCar();
holderConsultantNoCar.consultantWords = (TextView) convertView.findViewById(R.id.consultant_nocar);
holderConsultantNoCar.timeLong = (TextView) convertView.findViewById(R.id.consultant_nocar_time);
convertView.setTag(R.layout.consultant_nocar, holderConsultantNoCar);
break;
}
} else {
switch (type) {
case TYPE_1:
holderConsultantWords = (ViewHolderConsultantWords) convertView.getTag(R.layout.consultant_words);
break;
case TYPE_2:
holderRecommendCar = (ViewHolderRecommendCar) convertView.getTag(R.layout.consultant_recommend);
break;
case TYPE_3:
holderCustomWords = (ViewHolderCustomWords) convertView.getTag(R.layout.customer_words);
break;
case TYPE_4:
holderConsultantNoCar = (ViewHolderConsultantNoCar) convertView.getTag(R.layout.consultant_nocar);
break;
}
}
switch (type) {
case TYPE_1:
holderConsultantWords.consultantWords.setText(messageList.get(position).getMessage().getMessage());
holderConsultantWords.timeLong.setText(messageList.get(pos).getMessage().getDisplayTime());
break;
case TYPE_2:
ViewGroup.LayoutParams lp = holderRecommendCar.carImage.getLayoutParams();
lp.width = width;
lp.height = (width * 2 / 3);
System.out.println("width Message : " + lp.width + " height " + lp.height);
holderRecommendCar.carImage.setLayoutParams(lp);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holderRecommendCar.recommendCarRelative.getLayoutParams();
layoutParams.width = width;
holderRecommendCar.timeShort.setText(TLCommenUtil.getMessageTimeString(messageList.get(pos).getMessage().getDisplayDate(), true));
holderRecommendCar.timeLong.setText(TLCommenUtil.getMessageTimeString(messageList.get(pos).getMessage().getDisplayDate(), false));
if (messageList.get(pos).getCars() == null) {
break;
}
String carNum = Integer.toString(messageList.get(pos).getCars().size());
holderRecommendCar.carNum.setText(carNum);
holderRecommendCar.recommendCarRelative.setLayoutParams(layoutParams);
holderRecommendCar.recommendCarRelative.bringToFront();
holderRecommendCar.recommendCarRelative.setBackgroundResource(R.drawable.message_box_blue_bg);
if (TLCommenConstant.MESSAGE_UNREAD == messageList.get(pos).getMessage().getStatus()) {
holderRecommendCar.timeShort.setTextColor(context.getResources().getColor(R.color.tl_orange));
holderRecommendCar.carNum.setTextColor(context.getResources().getColor(R.color.tl_orange));
} else {
holderRecommendCar.timeShort.setTextColor(context.getResources().getColor(R.color.white));
holderRecommendCar.carNum.setTextColor(context.getResources().getColor(R.color.white));
}
// int carSize = messageList.get(pos).getCars().size();
// if (carSize > 1) {
// holderRecommendCar.alphaImage.setImageResource(alphaCardId[carSize - 2]);
// }
holderRecommendCar.recommendWords.setText(messageList.get(pos).getMessage().getMessage());
String picUrl = messageList.get(pos).getCars().get(0).getPictureBig();
ImageLoader.getInstance().displayImage(picUrl, holderRecommendCar.carImage, options, new ImageLoaderListener(holderRecommendCar));
if (position == timePosition && TLCommenConstant.MESSAGE_UNREAD != messageList.get(pos).getMessage().getStatus()) {
holderRecommendCar.timeCount.setVisibility(View.VISIBLE);
TimeCounter timer = new TimeCounter(currentTime, 1000, holderRecommendCar.timeCount);
if (!timeSetFlag) {
holderRecommendCar.timeCount.setText("");
timeSetFlag = true;
timer.start();
}
if (holderRecommendCar.timeCount.getText().length() < 2) {
timer = null;
holderRecommendCar.timeCount.setText("");
timer = new TimeCounter(currentTime, 1000, holderRecommendCar.timeCount);
timeSetFlag = true;
timer.start();
}
} else {
holderRecommendCar.timeCount.setVisibility(View.GONE);
}
break;
case TYPE_3:
holderCustomWords.customWords.setText(messageList.get(position).getMessage().getMessage());
holderCustomWords.timeLong.setText(TLCommenUtil.getMessageTimeString(messageList.get(pos).getMessage().getDisplayDate(), false));
break;
case TYPE_4:
holderConsultantNoCar.consultantWords.setText(messageList.get(position).getMessage().getMessage());
holderConsultantNoCar.timeLong.setText(TLCommenUtil.getMessageTimeString(messageList.get(pos).getMessage().getDisplayDate(), false));
break;
}
return convertView;
}
/**
* 判断是哪种消息 0为欢迎 1为推荐车辆 2为客户设置筛选
*
* @param type
* @return
*/
public int judgeMessageType(String type) {
if (TLCommenConstant.MESSAGE_TYPE_WELLCOME.equals(type)) return 0;
if (TLCommenConstant.MESSAGE_TYPE_CUSTOM_VIEW.equals(type)) return 1;
if (TLCommenConstant.MESSAGE_TYPE_CUSTOM_SET.equals(type)) return 2;
if (TLCommenConstant.MESSAGE_TYPE_NO_CAR.equals(type)) return 3;
return -1;
}
@Override
public int getItemViewType(int position) {
String messageType = messageList.get(position).getMessage().getType();
return judgeMessageType(messageType);
}
@Override
public int getViewTypeCount() {
return 4;
}
public class ImageLoaderListener implements ImageLoadingListener {
private ViewHolderRecommendCar holder;
ImageLoaderListener(ViewHolderRecommendCar holder) {
this.holder = holder;
}
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
String prefix = TLCommenConstant.MESSAGEFLOW_PREFIX;
if (loadedImage != null) {
Bitmap newBitmap = ImageTools.zoomBitmap(loadedImage, width, width * 2 / 3);
holder.carImage.setImageBitmap(newBitmap);
}
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
}
/**
* 计时器
*/
private class TimeCounter extends TLCountDownTimer {
public TextView v;
public long millisInFuture;
public TimeCounter(long millisInFuture, long countDownInterval, TextView v) {
super(millisInFuture, countDownInterval);
this.v = v;
this.millisInFuture = millisInFuture;
}
@Override
public void onFinish() {
v.setText("距下次推荐还有 00:00:00");
}
@Override
public void onTick(long millisUntilFinished) {
currentTime = millisUntilFinished;
long day1 = (millisUntilFinished / 1000) / (24 * 3600);
SimpleDateFormat sdf = new SimpleDateFormat("HH时mm分ss秒");
if (day1 == 0) {
String time = sdf.format(millisUntilFinished);
time = TLCommenUtil.getDisTimeFormat(millisUntilFinished);
v.setText("距下次推荐还有 : " + time);
} else {
String time = sdf.format(millisUntilFinished - (1000 * 24 * 3600));
time = TLCommenUtil.getDisTimeFormat(millisUntilFinished - (1000 * 24 * 3600));
v.setText("距下次推荐还有 " + day1 + "天: " + time);
}
}
}
}
多种布局应该用多个ViewHolder分开
有讲不对的地方请指正!