Android ListView 分类显示(一)单布局显示

1、对于静态数据,不是从后台获取的数据。此种类型数据已经有很明确分类且分类所在布局很简单(比如一个TextView),比如类别1下有两个子项目,类别2下有三个子项目。现在组装数据,一共有五条数据,每条数据都有一个isCategory自定义字段,用于标记是否为分类,继而对分类布局进行隐藏或显示即可。

2、根据日期年月日(同一天)进行数据的分类。(从后台获取)

public static class Photo implements Comparable<Photo> {
		public boolean isCategory;
		public int id;
		public String name;
		public long createTime;
		public String showCreateTime;
 
		@Override
		public int compareTo(Photo another) {
			if (this.createTime > another.createTime) {
				return -1;
			} else {
				if (this.createTime < another.createTime) {
					return 1;
				}
			}
			return 0;
		}
}
上面是一个照片的实体类,createTime时间为long型,用于排序,showCreateTime是经过格式化的日期(yyyy年MM月dd日)。需要在适配器里面添加一个更新数据的方法,主要功能是对数据进行排序和对isCategory字段进行赋值。


public class PhotoAdapter extends BaseAdapter {

	private List<Photo> infos = new ArrayList<Photo>;
	
	public void updateAdapter(List<Photo> foundRoots){
		if (foundRoots!=null && foundRoots.size()>0) {
			this.infos.addAll(foundRoots);
			Collections.sort(infos);
			String cateName=this.infos.get(0).getShowCreateTime(); //2015-02-14
			this.infos.get(0).setCategory(true);//第一个数据默认就是一个类别的日期
			for (int i=1;i<this.infos.size();i++) {
				if(cateName.equals( this.infos.get(i).getShowCreateTime())){ 
					this.infos.get(i).setCategory(false);//如果跟当前的类别相同,则不是类别
				}else {
					this.infos.get(i).setCategory(true);//如果不同,则是一个新的日期,就是一个类别
					cateName = this.infos.get(i).getShowCreateTime();
				}
			}
			
			notifyDataSetChanged();
		}
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final AlipayHolder holder;
    	if (convertView == null) {
			holder = new AlipayHolder();
			convertView = inflater.inflate(R.layout.photo_list_item, null);
			ViewUtils.inject(holder, convertView);
			convertView.setTag(holder);
		} else {
			holder = (AlipayHolder) convertView.getTag();
		}
    	
    	final Photo photo = (Photo)infos.get(position);
	 
		if (photo.isCategory()) {
			holder.iever_bigv_list_item_date.setText(photo.getShowCreateTime());
			holder.iever_bigv_list_item_date.setVisibility(View.VISIBLE);
		}else {
			holder.iever_bigv_list_item_date.setVisibility(View.GONE);
		}
		
		return convertView;

	}
	
	public final class AlipayHolder {
		@ViewInject(R.id.iever_bigv_user_description)
		public TextView iever_bigv_user_description;
		@ViewInject(R.id.iever_bigv_list_item_date)
		public TextView iever_bigv_list_item_date;

	}
	
}
 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical" >

	<!-- 分类标题布局 -->
    <LinearLayout
        android:id="@+id/iever_bigv_category"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#F2F2F2"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/line_date_left" >
        </ImageView>

        <TextView
            android:id="@+id/iever_bigv_list_item_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="5dip"
            android:text="title"
            android:textColor="#B3B3B3"
            android:textSize="@dimen/font_body_14" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/line_date_left" >
        </ImageView>
    </LinearLayout>
	<!-- 子项 布局 -->
	<LinearLayout 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:background="@color/white"
		android:orientation="vertical" >
		<ImageView
			android:layout_width="15dip"
			android:layout_height="20dip"
			android:src="@drawable/jiantou_right" />

		<TextView
			android:id="@+id/iever_bigv_user_description"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_gravity="center"
			android:text="title"
			android:textColor="@color/black"
			android:textSize="@dimen/font_body_16" />
    
	</LinearLayout>

</LinearLayout>



代码下载











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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值