package com.xuliang.multitypelistview;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MsgAdapter extends BaseAdapter {

	/*// 做图片又文本的布局
	private static final int TYPE_IMAGE_TEXT = 0;
	// 左文本又图片的布局
	private static final int TYPE_TEXT_IMAGE = 1;
	// 只显示图片的布局
	private static final int TYPE_IMAGE_ONLY = 2;
	// 只显示文本的布局
	private static final int TYPE_TEXT_ONLY = 3;
	// 布局的种类
	private static final int TYPE_COUNT = 4;*/

	private List<Msg> mList;

	private LayoutInflater mInflater;

	public MsgAdapter(Context context, List<Msg> mList) {
		this.mList = mList;

		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return mList.get(position).getmType();
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return Const.TYPE_COUNT;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int type = getItemViewType(position);
		Msg mMsg = mList.get(position);
		ImageTextHolder mItHolder = null;
		TextImageHolder mTiHolder = null;
		ImageOnlyHolder mIoHolder = null;
		TextOnlyHolder mToHolder = null;

		if (convertView == null) {
			switch (type) {
			case Const.TYPE_IMAGE_TEXT:
				mItHolder = new ImageTextHolder();
				convertView = mInflater.inflate(R.layout.image_text, null);
				mItHolder.mImageView = (ImageView) convertView
						.findViewById(R.id.image_text_iv);
				mItHolder.mTextView = (TextView) convertView
						.findViewById(R.id.image_text_tv);
				convertView.setTag(mItHolder);
				break;
			case Const.TYPE_TEXT_IMAGE:
				mTiHolder = new TextImageHolder();
				convertView = mInflater.inflate(R.layout.text_image, null);
				mTiHolder.mTextView = (TextView) convertView
						.findViewById(R.id.text_image_tv);
				mTiHolder.mImageView = (ImageView) convertView
						.findViewById(R.id.text_image_iv);
				convertView.setTag(mTiHolder);
				break;
			case Const.TYPE_IMAGE_ONLY:
				mIoHolder = new ImageOnlyHolder();
				convertView = mInflater.inflate(R.layout.image_only, null);
				mIoHolder.mImageView = (ImageView) convertView
						.findViewById(R.id.image_only_iv);
				convertView.setTag(mIoHolder);
				break;
			case Const.TYPE_TEXT_ONLY:
				mToHolder = new TextOnlyHolder();
				convertView = mInflater.inflate(R.layout.text_only, null);
				mToHolder.mTextView = (TextView) convertView
						.findViewById(R.id.text_only_tv);
				convertView.setTag(mToHolder);
				break;
			default:
				break;
			}
		} else {
			switch (type) {
			case Const.TYPE_IMAGE_TEXT:
				mItHolder = (ImageTextHolder) convertView.getTag();
				break;
			case Const.TYPE_TEXT_IMAGE:
				mTiHolder = (TextImageHolder) convertView.getTag();
				break;
			case Const.TYPE_IMAGE_ONLY:
				mIoHolder = (ImageOnlyHolder) convertView.getTag();
				break;
			case Const.TYPE_TEXT_ONLY:
				mToHolder = (TextOnlyHolder) convertView.getTag();
				break;
			default:
				break;
			}
		}
		switch (type) {
		case Const.TYPE_IMAGE_TEXT:
			mItHolder.mImageView.setImageResource(mMsg.getResourceId());
			mItHolder.mTextView.setText(mMsg.getName());
			break;
		case Const.TYPE_TEXT_IMAGE:
			mTiHolder.mTextView.setText(mMsg.getName());
			mTiHolder.mImageView.setImageResource(mMsg.getResourceId());
			break;
		case Const.TYPE_IMAGE_ONLY:
			mIoHolder.mImageView.setImageResource(mMsg.getResourceId());
			break;
		case Const.TYPE_TEXT_ONLY:
			mToHolder.mTextView.setText(mMsg.getName());
			break;
		default:
			break;
		}
		return convertView;
	}

	class ImageTextHolder {
		ImageView mImageView;
		TextView mTextView;
	}

	class TextImageHolder {
		TextView mTextView;
		ImageView mImageView;
	}

	class ImageOnlyHolder {
		ImageView mImageView;
	}

	class TextOnlyHolder {
		TextView mTextView;
	}

}

@@ -334,306 +334,8 @@
  %7B%0A%0A
-%09/*// %E5%81%9A%E5%9B%BE%E7%89%87%E5%8F%88%E6%96%87%E6%9C%AC%E7%9A%84%E5%B8%83%E5%B1%80%0A%09private static final int TYPE_IMAGE_TEXT = 0;%0A%09// %E5%B7%A6%E6%96%87%E6%9C%AC%E5%8F%88%E5%9B%BE%E7%89%87%E7%9A%84%E5%B8%83%E5%B1%80%0A%09private static final int TYPE_TEXT_IMAGE = 1;%0A%09// %E5%8F%AA%E6%98%BE%E7%A4%BA%E5%9B%BE%E7%89%87%E7%9A%84%E5%B8%83%E5%B1%80%0A%09private static final int TYPE_IMAGE_ONLY = 2;%0A%09// %E5%8F%AA%E6%98%BE%E7%A4%BA%E6%96%87%E6%9C%AC%E7%9A%84%E5%B8%83%E5%B1%80%0A%09private static final int TYPE_TEXT_ONLY = 3;%0A%09// %E5%B8%83%E5%B1%80%E7%9A%84%E7%A7%8D%E7%B1%BB%0A%09private static final int TYPE_COUNT = 4;*/%0A%0A
 %09pri

Change MsgAdapter.