使用自定义适配器实现ListView中的每一个list的不同显示风格

package com.kaiqi.apps_center.utils;

import java.util.List;
import java.util.Map;

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

import com.example.appscenter.R;

/**
 * 使用适配器实现一个ListView多个不同风格的list显示。
 * 最上面显示应用简介
 * 中间显示应用相关图片
 * 最下面显示应用详细介绍
 * @author ssrrxx
 *
 */
public class AppDetailListViewBaseAdapter extends BaseAdapter {
	private List<Map<String, Object>> list;
	private LayoutInflater inflater;
	//这里type不能用123,否则会越界
	private static final int TYPE_1 = 0;
	private static final int TYPE_2 = 1;
	private static final int TYPE_3 = 2;
	private Context context;

	public AppDetailListViewBaseAdapter(
			List<Map<String, Object>> list, Context context) {
		this.list = list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

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

	/**
	 * 返回布局类型种数,布局会根据种数与下面的方法配合具体怎样实现布局
	 */
	@Override
	public int getViewTypeCount() {
		return list.size();
	}

	/**
	 * 根据item所在位置返回布局类型
	 * 该方法的作用是,根据上中下的位置,可能有超过4中的布局类型。分别调用不同的布局类型,在getView方法中根据TYPE进行相应的布局
	 */
	@Override
	public int getItemViewType(int position) {
		if (position  == 0) {
			return TYPE_1;
		} else if (position  == 1) {
			return TYPE_2;
		} else {
			return TYPE_3;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder1 holder1 = null;
		ViewHolder2 holder2 = null;
		ViewHolder3 holder3 = null;

		int type = this.getItemViewType(position);

		switch (type) {
		case TYPE_1:
			convertView = inflater.inflate(R.layout.app_detail_1, null);
			holder1 = new ViewHolder1();
			holder1.ico = (ImageView) convertView.findViewById(R.id.app_ico);
			System.out.println(holder1.ico);
			holder1.appName = (TextView) convertView
					.findViewById(R.id.app_name);
			holder1.appSize = (TextView) convertView
					.findViewById(R.id.app_size);
			holder1.appUpdateDate = (TextView) convertView
					.findViewById(R.id.app_update_date);
			holder1.appVersion = (TextView) convertView
					.findViewById(R.id.app_version);
			holder1.appClass = (TextView) convertView
					.findViewById(R.id.app_class);
			convertView.setTag(holder1);
			holder1.ico.setImageResource(Integer.parseInt((String)list.get(position).get(
					"appIco")));
			holder1.appName.setText((String) list.get(position).get("appName"));
			holder1.appSize.setText("大小:"
					+ (String) list.get(position).get("appSize"));
			holder1.appUpdateDate.setText("更新:"
					+ (String) list.get(position).get("updateDate"));
			holder1.appVersion.setText("版本:"
					+ (String) list.get(position).get("version"));
			holder1.appClass.setText("分类:"
					+ (String) list.get(position).get("appClass"));
			break;
		case TYPE_2:
			convertView = inflater.inflate(R.layout.app_detail_2, null);
			holder2 = new ViewHolder2();
			holder2.gallery = (Gallery) convertView.findViewById(R.id.app_pics);
			int[] imageIds = (int[]) list.get(position).get("imageIds");
			MyGalleryBaseAdapter adapter = new MyGalleryBaseAdapter(imageIds,
					context);
			holder2.gallery.setAdapter(adapter);
			Log.v("type", "type2");
			break;
		case TYPE_3:
			convertView = inflater.inflate(R.layout.app_detail_3, null);
			holder3 = new ViewHolder3();
			holder3.appIntroduction = (TextView) convertView
					.findViewById(R.id.app_introduction);
			Log.v("type", "type3");
			holder3.appIntroduction.setText((String) list.get(position).get(
					"appIntroduction"));
		}

		return convertView;
	}

	static class ViewHolder1 {
		ImageView ico;
		TextView appName;
		TextView appSize;
		TextView appUpdateDate;
		TextView appVersion;
		TextView appClass;
	}

	static class ViewHolder2 {
		Gallery gallery;
	}

	static class ViewHolder3 {
		TextView appIntroduction;
	}
}


 

你可能感兴趣的:(使用自定义适配器实现ListView中的每一个list的不同显示风格)