其中日期标题部分视图布局:

带图片的条目布局部分:

问题在于,如何在ListView中既有标题条目又有内容条目。

这里用到了设计模式中的Iterator模式。在java代码中示例有Iterator,可以迭代ArrayList,HashSet等不同的数据结构对象。

ListElement是接口:

  1. package com.easymorse.listview.customer;      
    
     import android.content.Context;    
    
     import android.view.LayoutInflater;    
    
    import android.view.View;    
    
     public interface ListElement {    
    
     public int getLayoutId();    
    
     public boolean isClickable();    
    
     public View getViewForListElement(LayoutInflater layoutInflater,    
    
     Context context, View view);    
    
    }
      

其中:

  • getLayoutId()返回布局的值;
  • isClickable()返回是否可点击;
  • getViewForListElement()返回视图对象。

这个接口有两个实现:

  • SectionListElement,用于实现标题条目;
  • ContentListElement,用于实现内容条目。

见SectionListElement代码:

  1. package com.easymorse.listview.customer;      
    
     import android.content.Context;    
    
     import android.view.LayoutInflater;    
    
     import android.view.View;    
    
     import android.widget.LinearLayout;    
    
     import android.widget.TextView;    
    
     public class SectionListElement implements ListElement {    
    
     private String text;    
    
    public void setText(String text) {    
    
     this.text = text;    
    
     }    
    
     @Override   
    
     public int getLayoutId() {    
    
     return R.layout.section;    
    
     }    
    
     @Override   
    
     public boolean isClickable() {    
    
     return false;    
    
     }       
    
     @Override   
    
    public View getViewForListElement(LayoutInflater layoutInflater,    
    
     Context context, View view) {    
    
     LinearLayout layout = (LinearLayout) layoutInflater.inflate(getLayoutId(), null);    
    
    TextView textView=(TextView) layout.findViewById(R.id.section_title);    
    
     textView.setText(text);    
    
     return layout;    
    
    }    
    
     }
     

见ContentListElement代码:

ListView需要ListAdapter的实现。在这里是直接集成BaseAdapter来实现的。用于交给ListView生成出列表。代码:

在Activity中创建CustomerListAdapter以及设置它的代码部分:

CustomerListAdapter adapter = new CustomerListAdapter(this);    

 adapter.addSectionHeaderItem(“2002-3-1″);    

 ArrayList<ListElement> elements = new ArrayList<ListElement>();    

 for (int i = 0; i < 5; i++) {    

ContentListElement element = new ContentListElement();    

 element.setTitle(“哈利波特第” + (i+1) + “集”);    

elements.add(element);    

}    

 adapter.addList(elements);    

adapter.addSectionHeaderItem(“2002-2-2″);    

 elements = new ArrayList<ListElement>();    

 for (int i = 0; i < 3; i++) {    

ContentListElement element = new ContentListElement();    

 element.setTitle(“指环王第” + (i+1) + “集”);    

elements.add(element);    

}    

 adapter.addList(elements);    

 this.setListAdapter(adapter);   

这里ListActivity,还需要注意两件事情,Activity要继承ListActivity。另外,在layout中:

ListView的id要用系统自带的

Listview Section 多个标题以及内容的更多相关文章

  1. easyui中datagrid标题居中内容居左实现方式

    easyui中的datagrid使用起来,确实还是挺轻巧方便,但是其中也有不少的问题,尤其遇到客户的一些特殊的需求时往往实现得不是很好.这个时候就需要我们自己动手来修改easyui的源码了.easyu ...

  2. WPF自学入门(六)WPF带标题的内容控件简单介绍

    在WPF自学入门(二)WPF-XAML布局控件的文章中分别介绍StackPanel,WarpPanel,DockPanel,Grid,Canvas五种布局容器的使用,可以让我们大致了解容器可以使用在什 ...

  3. dedecms在后台替换文章标题、内容、摘要、关键字

    dedecms在后台替换文章标题.内容.摘要.关键字所在的字段为: 后台替换文章内容 数据表:dede_addonarticle 字段:body 后台替换文章摘要内容 数据表:dede_archive ...

  4. C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,支持doc,xls,ppt,pdf,txt等格式的文件搜索

    C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,指定目录后,遍历搜索文件和子目,现在只写了支持.DOC.DOCX.XLS.XLSX.PPT.PPTX.PDF.HTML.HTM.TXT等 ...

  5. Python3:爬取新浪、网易、今日头条、UC四大网站新闻标题及内容

    Python3:爬取新浪.网易.今日头条.UC四大网站新闻标题及内容 以爬取相应网站的社会新闻内容为例: 一.新浪: 新浪网的新闻比较好爬取,我是用BeautifulSoup直接解析的,它并没有使用J ...

  6. 原生js制作标题与内容保持4行的效果

    在制作网页或移动端有时会用到一个效果,类似文章标题和文章描述的排列总是保持一样的行数,要么标题总是一行,多出的省略,要么标题内容1:3或2:2或3:1这样,今天练习这样的效果. 实现的原理:给标题和内 ...

  7. JQuery datatables 标题和内容居中显示

    1.如题,使用到了强大的表格插件datatables,要使标题和内容都居中显示,只需要在jsp引入css,写上如下内容即可: /*qiulinhe:2016年11月7日13:48:26*/ /* da ...

  8. 解决boostrap-table有水平和垂直滚动条时,滚动条滑到最右边表格标题和内容单元格无法对齐的问题

    问题:boostrap-table有水平和垂直滚动条时,滚动条不高的时候(滚动高度比较大的时候没有问题),滚动条滑到最右边表格标题和内容单元格无法对齐的问题 问题原因:bootstrap-table源 ...

  9. 利用scrapy获取抽屉新热榜的标题和内容以及新闻地址保存到本地

    1.安装scrapy pip3 install scrapy 2.打开terminal,cd 到想要创建程序的目录下 3.创建一个scrapy项目 在终端输入:scrapy startproject ...

随机推荐

  1. axis client error Bad envelope tag: definitions

    http://blog.csdn.net/lifuxiangcaohui/article/details/8090503 ——————————————————————————————————————— ...

  2. font-face自定义字体

    做网站的时候,有时候会遇到某些字体系统里面没有自带.可能更多的时候我们会选择以图替文的方式来做.用图片的话不利于图片的放大缩小,更好的办法是我们可以自定义字体. 当然,在实际运用中我们需要权衡一下自定 ...

  3. css鼠标移动到文字上怎样变化背景颜色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. DIV+CSS 命名规范

    常用的CSS命名规则: 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar栏目:column 页面外围控制整体布局宽度:wrapper 左 ...

  5. jquery 新建的元素事件绑定问题研究[转]

    原文:http://www.cnblogs.com/linzheng/archive/2010/10/17/1853568.html js的事件监听跟css不一样,css只要设定好了样式,不论是原来就 ...

  6. TopK的一个简单实现

    转自:http://rangerwolf.iteye.com/blog/2119096 题外话: <Hadoop in Action> 是一本非常不错的交Hadoop的入门书,而且建议看英 ...

  7. python with妙用

    class aa(): def bb(self): print("hhhh") return "hello world" def __enter__(self) ...

  8. MySQL创建用户、授权、撤销权限、删除用户

    一.创建用户 mysql -u root -p 1.mysql->create user 'test'@'localhost' identified by '123'; 2.mysql-> ...

  9. e659. 缩放,剪取,移动,翻转图形

    AffineTransform tx = new AffineTransform(); tx.scale(scalex, scaley); tx.shear(shiftx, shifty); tx.t ...

  10. 视觉SLAM之词袋(bag of words) 模型与K-means聚类算法浅析(1)

    在目前实际的视觉SLAM中,闭环检测多采用DBOW2模型https://github.com/dorian3d/DBoW2,而bag of words 又运用了数据挖掘的K-means聚类算法,笔者只 ...