安卓学习之路—ListView的分栏显示 —标识元素类型

实现如图效果

1.4个实体类,TitleList,Person,Animal,Plant都继承抽象类ListView1(含有实体类的get,set,方法)

<pre name="code" class="java">public class TitleList extends ListView1{
	private String title;
	private String count;
	//标识条目类型
	public final static int listType=0;


 
public class Person extends ListView1{
	private String name;
	private String age;
	public final static int listType=1;}
public class Animal extends ListView1{
	private String name;
	private String age;
	private String color;
	public final static  int listType=2;}
public class Plant extends ListView1{
	private String name;
	private String local;
	private int con;
	public final static int listType=3;}


2.按所需顺序将对象加入到集合, 数据处理完后通知主线程给ListView设置适配器


new Thread() {

			
			public void run() {

				List<ListView1> totals = new ArrayList<ListView1>();
				List<ListView1> persons = new ArrayList<ListView1>();
				List<ListView1> animals = new ArrayList<ListView1>();
				List<ListView1> plants = new ArrayList<ListView1>();
				for (int i = 0; i < 10; i++) {
					Person person = new Person("person" + i, "age" + i);
					Plant plant = new Plant(" plant" + i, "local" + i,
							R.drawable.ic_launcher);
					Animal animal = new Animal("name" + i, "age" + i, "color"
							+ i);
					persons.add(person);
					animals.add(animal);
					plants.add(plant);
				}
<span style="white-space:pre">				</span>//标题栏
				TitleList titleList = new TitleList("人类", persons.size() + "");
				totals.add(titleList);
				totals.addAll(persons);
				TitleList titleList2 = new TitleList("动物", animals.size() + "");
				totals.add(titleList2);
				totals.addAll(animals);
				TitleList titleList3 = new TitleList("植物", plants.size() + "");
				totals.add(titleList3);
				totals.addAll(plants);
				handler.obtainMessage(LIST_SUCCESS, totals).sendToTarget();
			};
		}.start();

3适配器,重写 getViewTypeCount getItemViewType

class MyAdapter extends BaseAdapter {

		private List<ListView1> lists;

		/**
		 * @return the lists
		 */
		public List<ListView1> getLists() {
			return lists;
		}

		/**
		 * @param lists
		 *            the lists to set
		 */
		public void setLists(List<ListView1> lists) {
			this.lists = lists;
		}

		public MyAdapter(List<ListView1> lists) {
			this.lists = lists;
		}

		@Override
		public int getCount() {

			return lists == null ? 0 : lists.size();
		}

		@Override
		public Object getItem(int position) {

			return null;
		}

		@Override
		public long getItemId(int position) {

			return 0;
		}

		@Override
		public int getViewTypeCount() {

			return 4;// 告诉适配器,条目有几种类型
		}

		@Override
		public int getItemViewType(int position) {

			return lists.get(position).getListType();// 告诉适配器当前条目类型
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ListView1 view1 = lists.get(position);
<span style="white-space:pre">			</span>//当前返回的条目类型
			int listType = getItemViewType(position);
			HolderView hw = null;
			switch (listType) {
			// 集合中的元素如果是标题类型
			case TitleList.listType:
				if (convertView == null) {
					hw = new HolderView();
					convertView = View.inflate(MainActivity.this,
							R.layout.title_item, null);
					hw.tv_title = (TextView) convertView
							.findViewById(R.id.tv_title);
					convertView.setTag(hw);
				} else {
					hw = (HolderView) convertView.getTag();
				}
				hw.tv_title.setText(view1.getTitle() + " 个数" + view1.getCount());
				break;
			// 集合中的元素如果是人类型
			case Person.listType:

				if (convertView == null) {
					hw = new HolderView();
					convertView = View.inflate(MainActivity.this,
							R.layout.person_item, null);
					hw.tv_person = (TextView) convertView
							.findViewById(R.id.tv_person);
					convertView.setTag(hw);
				} else {
					hw = (HolderView) convertView.getTag();
				}
				hw.tv_person.setText(view1.getName() + " 年龄:" + view1.getAge());
				break;
			// 集合中的元素如果是动物类型
			case Animal.listType:

				if (convertView == null) {
					hw = new HolderView();
					convertView = View.inflate(MainActivity.this,
							R.layout.animal_item, null);
					hw.tv_animal = (TextView) convertView
							.findViewById(R.id.tv_animal);

					convertView.setTag(hw);

				} else {
					hw = (HolderView) convertView.getTag();

				}

				hw.tv_animal.setText(view1.getName() + " 年龄:" + view1.getAge()
						+ " 颜色:" + view1.getColor());
				break;
			// 集合中的元素如果是植物类型
			case Plant.listType:

				if (convertView == null) {
					hw = new HolderView();
					convertView = View.inflate(MainActivity.this,
							R.layout.plant_item, null);
					hw.tv_plant = (TextView) convertView
							.findViewById(R.id.tv_plant);
					convertView.setTag(hw);
				} else {
					hw = (HolderView) convertView.getTag();
				}
				hw.tv_plant
						.setText(view1.getName() + " 地点:" + view1.getLocal());
				break;
			default:
				break;
			}

			return convertView;
		}

	}
// view持有者,减少findviewbyid
	class HolderView {
		private TextView tv_title;
		private TextView tv_person;
		private TextView tv_animal;
		private TextView tv_plant;

		public HolderView() {

		}

	}









http://www.cnblogs.com/yizuochengchi2012/p/5050004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值