最近项目中需要做一个按标题分组的ListView,要实现的效果如下图

标题是月份,在按月份的数据是改月份的账单数据。开始想的就是在列表的某个位置插入另一个布局,其他的地方和普通的ListView一样。并不知道该在那个位置插入这些标题。所以需要在数据上进行处理,另外需要知道每个item位置应该用什么类型的item view。


具体实现如下:

1.先对数据进行处理:
需要处理的json数据格式如下图 , billList数组是每个需要展示的账单item,month字段是一个组item标题。

根据数据和展示的需求,创建一个数据实体类,代码如下:

 public static class ContentBean {private String month; //存储每组数据的月份标题private List<BillListResponse.ContentBean.BillListBean> billList;//一组数据的list列表

ListView的适配器(关键)

public class AllRecordBillAdapter extends BaseAdapter{//需要展示的数据 , 多组数据的集合List<BillListResponse.ContentBean> mRecordContentList;private Context mContext;private LayoutInflater mInflater;//类别的ITEM 月份private static final int TYPE_ITEM = 0;//具体账单内容private static final int CONTENT_ITEM = 1;public  AllRecordBillAdapter(Context  context  , List<BillListResponse.ContentBean> billRecordBeanList) {mRecordContentList = billRecordBeanList;mContext = context;mInflater = LayoutInflater.from(mContext);}@Overridepublic int getCount() {//计算出所有的要展示的数据item的个数,包括每一个int count = 0;if (null != mRecordContentList) {//  所有分类中item的总个数for (BillListResponse.ContentBean contentBean : mRecordContentList) {count += (contentBean.getBillList().size()+1);}}return count;}@Overridepublic Object getItem(int position) {// 异常情况处理if (null == mRecordContentList || position <  0 || position > getCount()) {return null;}//同一分类内,第一个元素的索引值int groupFirstIndex = 0;for (BillListResponse.ContentBean contentBean : mRecordContentList) {int size = contentBean.getBillList().size()+1;// 在当前分类中的索引值int categoryIndex = position - groupFirstIndex;// item在当前分类内if (categoryIndex < size) {if(categoryIndex == 0){//首选项返回的是月份return   contentBean.getMonth();}else{//返回其他数据return  contentBean.getBillList().get(categoryIndex-1);}}// 索引移动到当前分类结尾,即下一个分类第一个元素索引groupFirstIndex += size;}return null;}@Overridepublic int getItemViewType(int position) {// 异常情况处理if (null == mRecordContentList || position <  0|| position > getCount()) {return TYPE_ITEM;}int groupFirstIndex = 0;for (BillListResponse.ContentBean contentBean : mRecordContentList){int size = contentBean.getBillList().size()+1;// 在当前分类中的索引值int categoryIndex = position - groupFirstIndex;//分组的标题if (categoryIndex == 0) {return TYPE_ITEM;}groupFirstIndex += size;}//内容项return CONTENT_ITEM;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic int getViewTypeCount() {return 2;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {int itemViewType = getItemViewType(position);switch (itemViewType) {case TYPE_ITEM:if (null == convertView) {convertView = mInflater.inflate(R.layout.bill_record_item_header, null);}TextView textView = (TextView) convertView.findViewById(R.id.bill_record_header);String  itemValue = (String) getItem(position);textView.setText(itemValue);break;case CONTENT_ITEM:RecordViewHolder viewHolder = null;if (null == convertView) {convertView = mInflater.inflate(R.layout.bill_record_content_item, null);viewHolder = new RecordViewHolder();viewHolder.contentWeek = (TextView) convertView.findViewById(R.id.bill_record_week_info);viewHolder.contentDay = (TextView) convertView.findViewById(R.id.bill_record_day_info);viewHolder.contentAmount = (TextView) convertView.findViewById(R.id.bill_record_amount_info);viewHolder.contentType = (TextView) convertView.findViewById(R.id.bill_record_type_info);convertView.setTag(viewHolder);} else {viewHolder = (RecordViewHolder) convertView.getTag();}// 绑定数据BillListResponse.ContentBean.BillListBean billListBean = (BillListResponse.ContentBean.BillListBean)getItem(position);String createTime = billListBean.getCreateTime();String date = createTime.substring(5, 10);String week = dayForWeek(createTime.substring(0,10));viewHolder.contentWeek.setText(week);viewHolder.contentType.setText(billListBean.getItemName());viewHolder.contentDay.setText(date);viewHolder.contentAmount.setText(billListBean.getPrice()+"");break;}return convertView;}@Overridepublic boolean areAllItemsEnabled() {return false;}@Overridepublic boolean isEnabled(int position) {return getItemViewType(position) != TYPE_ITEM;}class  RecordViewHolder {TextView contentWeek;TextView contentDay;TextView contentAmount;TextView contentType;}public static String dayForWeek(String pTime){SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");Calendar c = Calendar.getInstance();try {c.setTime(format.parse(pTime));} catch (ParseException e) {e.printStackTrace();}int dayForWeek = 0;if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){dayForWeek = 7;}else{dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;}if(dayForWeek == 1) {return "周一";}else if(dayForWeek == 2){return "周二";}else if(dayForWeek == 3){return "周三";}else if(dayForWeek == 4){return "周四";} else if(dayForWeek == 5){return "周五";}else if(dayForWeek == 6){return "周六";}else if(dayForWeek == 7){return "周日";}return null;}

按标题分组显示的ListView相关推荐

  1. 分组显示的ListView分页加载数据

    参考:  http://www.cnblogs.com/qianxudetianxia/archive/2011/06/07/2074326.html 分组的ListView的拖拽  http://w ...

  2. ALV 层级分组显示报表

    相当于分组显示.第一行显示分组的头,下面显示明细. 例程1:采购订单主从表ALV层次输出 *&------------------------------------------------- ...

  3. 按钮 每一行_word跨页表格如何重复设置表头?单击“重复标题行”按钮来设置多页表格重复标题行显示。...

    教师们经常需要将一张表格需要在文档中多页跨页显示,则设置标题行重复显示很有必要,因为这样会在每一页都明确显示表格中的每一列所代表的实际意义,方便我们查找和校对数据. 第一步:打开需要设置表格标题行重复 ...

  4. 2、HTML——标题分组、居中、引用标签、水平线标签下划线标签、删除标签、<font>标签、图像标签

    目录 一.基本标签 1.标题分组:hgroup 2.居中:center 3.引用标签 3.1 块(长)引用标签:blockquote 3.2  短引用标签:q 4.水平线标签:hr 5.下划线标签:i ...

  5. Flutter Dart:用数字分组显示大数字

    Flutter & Dart:用数字分组显示大数字 大家好,我是坚果,我的公众号"坚果前端", 用逗号显示大数字作为千位分隔符将增加可读性.这篇简短的文章将向您展示如何借助 ...

  6. 【Matlab】你想知道在图表标题中显示变量的一切

    1.Introduction 有时在利用Matlab输出结果时,会通过作图(Figure),和做表(Excel)来完成.并且经常需要在标题中显示一些变量,比如下图,那么这是怎么实现的呢?能不能显示数组 ...

  7. DEDE无简略标题时显示完整标题

    新闻的标题需要进行字数限制,这就需要加入一个title属性,让鼠标放上去的时候显示完整标题.另外目前的调用只能同时调用一种标题方式,不过可 以采用以下方法,进行判断,无简略标题显示完整标题.例如ded ...

  8. birt报表的行隐藏和列隐藏以及分组显示

    为什么80%的码农都做不了架构师?>>>    1.行隐藏 按照图中的步骤:1.选择要隐藏的行:2.在property Editor 中找到visibility选项,勾上HIde E ...

  9. 安卓学习之路之如何显示一个listview列表视图

    1.新建个安卓项目,点击res下的values目录,在该目录下创建一个名字为arrays.xml的Android xml文件,然后开始在里面写代码,代码如下: <?xml version=&qu ...

最新文章

  1. 图片上传压缩android,android 图片上传压缩常见问题分析
  2. gitlab bash_如何编写Bash一线式以克隆和管理GitHub和GitLab存储库
  3. 线程打印状态_Java线程状态的转换
  4. Tomcat 启动耗时严重
  5. 零基础如何学前端,如何规划?
  6. 任正非华为为什么暂不推出鸿蒙,任正非表示,华为的鸿蒙系统已经上网?惊喜吗...
  7. 特斯拉中国工厂2020投产,还可能为完全自动驾驶更新硬件
  8. 数字电子技术基础(五):编码器
  9. Android开源项目:微信打飞机游戏源代码
  10. 基于Leaflet 的Web地图客户端应用程序开发框架
  11. policy服务器未能登陆,开机出现Group Policy Client服务未能登录的解决措施
  12. 再给大家推荐一些小游戏
  13. 有信仰与无信仰家族200年对比,让世人震惊!!
  14. S100数字源表之LDO芯片电学特性测试方案
  15. Fuz OJ 2116 买糖果
  16. chrome cookie存在什么地方_谷歌浏览器产生的cookie缓存怎么查看呢?Cookie的具体内容在哪里...
  17. 怎么释放C盘空间?清理C盘空间的4大方法分享!
  18. ORY Hydra项目详解
  19. 华三服务器hdm时间怎么修改,华三服务器HDM命令设置
  20. 【转】蓝牙通信-打开和关闭蓝牙设备

热门文章

  1. Git系列——win 10配置git环境
  2. Android动画之图片动画(四)
  3. 谷歌Android手机进入初步的收获期,后续市场如何?
  4. 变色龙常用参数的意义说明
  5. 通用串行总线驱动失效
  6. RequestContextHolder跨线程获取不到request对象,解决方法
  7. ReactNative利用Animated(动画)实现颜色(backgroundColor)的动态变化
  8. 服务的注册与发现(Eureka)
  9. java多线程聊天室_【Java】Socket+多线程实现控制台聊天室
  10. 纯css实现div同行显示