Listview 多个ViewHolder实现

简单代码示例:

 

package com.edaixi.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.edaixi.activity.R;
import com.edaixi.dataset.MyAddressDataSet;

public class SelectAddressListAdapter extends BasicAdapter {
	private MyAddressDataSet mTrueDataSet = null;
	private MyAddressDataSet mFalseDataSet = null;
	private Context context;
	public static int TRUE_TITLE = 1;
	public static int TRUE_ITEM = 2;

	public SelectAddressListAdapter(Context mContext,
			MyAddressDataSet mTrueDataSet, MyAddressDataSet mFalseDataSet) {
		super(mContext);
		this.mTrueDataSet = mTrueDataSet;
		this.mFalseDataSet = mFalseDataSet;
		this.context = mContext;
	}

	@Override
	public int getCount() {
		if (mTrueDataSet.size() > 0 && mFalseDataSet.size() > 0) {
			return mTrueDataSet.size() + mFalseDataSet.size() + 2;
		} else if (mTrueDataSet.size() == 0 && mFalseDataSet.size() == 0) {
			return mTrueDataSet.size() + mFalseDataSet.size();
		} else {
			return mTrueDataSet.size() + mFalseDataSet.size() + 1;
		}
	}

	@Override
	public Object getItem(int position) {
		if (position == 0) {
			return null;
		} else if (position < mTrueDataSet.size() + 1) {
			return mTrueDataSet.getIndexBean(position);
		} else {
			return mFalseDataSet.getIndexBean(position - mTrueDataSet.size()
					- 2);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {

		if ((mTrueDataSet.size() > 0) && (position == 0)) {
			return TRUE_TITLE;
		} else if ((mFalseDataSet.size() > 0)
				&& (position == mTrueDataSet.size() + 1)) {
			return TRUE_TITLE;
		} else {
			return TRUE_ITEM;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderItem mViewHolder = null;
		ViewHolderTitle mViewHolderTitle = null;
		final int _position = position;
		int viewType = getItemViewType(position);
		if (convertView == null) {
			if (viewType == 2) {
				mViewHolder = new ViewHolderItem();
				convertView = View.inflate(context,
						R.layout.item_address_select, null);
				mViewHolder.mName = (TextView) convertView
						.findViewById(R.id.address_item_name);
				mViewHolder.mPhone = (TextView) convertView
						.findViewById(R.id.address_item_tel);
				mViewHolder.mDetailAdd = (TextView) convertView
						.findViewById(R.id.address_item_text);
				mViewHolder.mAddLayout = (RelativeLayout) convertView
						.findViewById(R.id.rl_address_select);
				convertView.setTag(mViewHolder);
			} else {
				mViewHolderTitle = new ViewHolderTitle();
				convertView = View.inflate(context,
						R.layout.address_select_true_tips_title, null);
				mViewHolderTitle.mTitleName = (TextView) convertView
						.findViewById(R.id.tv_address_title);
				convertView.setTag(mViewHolderTitle);
			}
		} else {
			if (viewType == 2) {
				mViewHolder = (ViewHolderItem) convertView.getTag();
			} else {
				mViewHolderTitle = (ViewHolderTitle) convertView.getTag();
			}
		}

		if ((mTrueDataSet.size() > 0) && (position == 0)) {
			mViewHolderTitle.mTitleName.setText("可用地址");
		} else if ((mFalseDataSet.size() > 0)
				&& (position == mTrueDataSet.size() + 1)) {
			mViewHolderTitle.mTitleName.setText("不可用地址");
		} else {
			if (position <= mTrueDataSet.size()) {
				mViewHolder.mAddLayout.setBackgroundColor(context
						.getResources().getColor(R.color.white));
				mViewHolder.mName.setTextColor(context.getResources().getColor(
						R.color.text_1));
				mViewHolder.mName.setText(mTrueDataSet.getIndexBean(
						_position - 1).getUsername());

				if (mTrueDataSet.getIndexBean(_position - 1).getTel() != null) {
					mViewHolder.mPhone.setTextColor(context.getResources()
							.getColor(R.color.text_1));
					mViewHolder.mPhone.setText(mTrueDataSet.getIndexBean(
							_position - 1).getTel());
				}
				mViewHolder.mDetailAdd.setTextColor(context.getResources()
						.getColor(R.color.text_1));
				if (mTrueDataSet.getIndexBean(_position - 1).getAddress() != null)
					mViewHolder.mDetailAdd.setText(mTrueDataSet.getIndexBean(
							_position - 1).getCity()
							+ mTrueDataSet.getIndexBean(_position - 1)
									.getArea()
							+ mTrueDataSet.getIndexBean(_position - 1)
									.getAddress().trim());

			} else {
				mViewHolder.mAddLayout.setBackgroundColor(context
						.getResources().getColor(R.color.gray));
				mViewHolder.mName.setTextColor(context.getResources().getColor(
						R.color.falseaddress));
				mViewHolder.mName.setText(mFalseDataSet.getIndexBean(
						position - mTrueDataSet.size() - 2).getUsername());

				if (mFalseDataSet.getIndexBean(
						position - mTrueDataSet.size() - 2).getTel() != null) {
					mViewHolder.mPhone.setTextColor(context.getResources()
							.getColor(R.color.falseaddress));
					mViewHolder.mPhone.setText(mFalseDataSet.getIndexBean(
							position - mTrueDataSet.size() - 2).getTel());
				}
				mViewHolder.mDetailAdd.setTextColor(context.getResources()
						.getColor(R.color.falseaddress));
				if (mTrueDataSet.getIndexBean(
						_position - mTrueDataSet.size() - 2).getAddress() != null)
					mViewHolder.mDetailAdd.setText(mFalseDataSet.getIndexBean(
							position - mTrueDataSet.size() - 2).getCity()
							+ mFalseDataSet.getIndexBean(
									position - mTrueDataSet.size() - 2)
									.getArea()
							+ mFalseDataSet
									.getIndexBean(
											position - mTrueDataSet.size() - 2)
									.getAddress().trim());
			}
		}
		return convertView;
	}

	private static class ViewHolderItem {
		private TextView mName;
		private TextView mPhone;
		private TextView mDetailAdd;
		private RelativeLayout mAddLayout;
	}

	private static class ViewHolderTitle {
		private TextView mTitleName;
	}
}

  

posted @ 2015-07-04 20:05  狂奔的小狮子  阅读(385)  评论(0编辑  收藏  举报