前几天,和ios开发的同事扯淡时发现iphone里有个section listview,分章节的列表。android中的联系人也有这种效果,首字母相同的联系人会被分在一个章节中。

后来搜了一下,android做起来也很easy。下面记录一下方便以后参考(大家改一下包名)

首先复写一下BaseAdapter:

[java] view
plain
copy

  1. package com.test.activity;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import android.content.Context;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.Adapter;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.BaseAdapter;
  10. public class SeparatedListAdapter extends BaseAdapter {
  11. public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
  12. public final ArrayAdapter<String> headers;
  13. public final static int TYPE_SECTION_HEADER = 0;
  14. public SeparatedListAdapter(Context context) {
  15. headers = new ArrayAdapter<String>(context, R.layout.list_header);
  16. }
  17. public void addSection(String section, Adapter adapter) {
  18. this.headers.add(section);
  19. this.sections.put(section, adapter);
  20. }
  21. public Object getItem(int position) {
  22. for (Object section : this.sections.keySet()) {
  23. Adapter adapter = sections.get(section);
  24. int size = adapter.getCount() + 1;
  25. // check if position inside this section
  26. if (position == 0)
  27. return section;
  28. if (position < size)
  29. return adapter.getItem(position - 1);
  30. // otherwise jump into next section
  31. position -= size;
  32. }
  33. return null;
  34. }
  35. public int getCount() {
  36. // total together all sections, plus one for each section header
  37. int total = 0;
  38. for (Adapter adapter : this.sections.values())
  39. total += adapter.getCount() + 1;
  40. return total;
  41. }
  42. public int getViewTypeCount() {
  43. // assume that headers count as one, then total all sections
  44. int total = 1;
  45. for (Adapter adapter : this.sections.values())
  46. total += adapter.getViewTypeCount();
  47. return total;
  48. }
  49. public int getItemViewType(int position) {
  50. int type = 1;
  51. for (Object section : this.sections.keySet()) {
  52. Adapter adapter = sections.get(section);
  53. int size = adapter.getCount() + 1;
  54. // check if position inside this section
  55. if (position == 0)
  56. return TYPE_SECTION_HEADER;
  57. if (position < size)
  58. return type + adapter.getItemViewType(position - 1);
  59. // otherwise jump into next section
  60. position -= size;
  61. type += adapter.getViewTypeCount();
  62. }
  63. return -1;
  64. }
  65. public boolean areAllItemsSelectable() {
  66. return false;
  67. }
  68. public boolean isEnabled(int position) {
  69. return (getItemViewType(position) != TYPE_SECTION_HEADER);
  70. }
  71. @Override
  72. public View getView(int position, View convertView, ViewGroup parent) {
  73. int sectionnum = 0;
  74. for (Object section : this.sections.keySet()) {
  75. Adapter adapter = sections.get(section);
  76. int size = adapter.getCount() + 1;
  77. // check if position inside this section
  78. if (position == 0)
  79. return headers.getView(sectionnum, convertView, parent);
  80. if (position < size)
  81. return adapter.getView(position - 1, convertView, parent);
  82. // otherwise jump into next section
  83. position -= size;
  84. sectionnum++;
  85. }
  86. return null;
  87. }
  88. @Override
  89. public long getItemId(int position) {
  90. return position;
  91. }
  92. }

然后开始写主Act,用listview适配一下上面的adapter

[java] view
plain
copy

  1. package com.test.activity;
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ListView;
  10. import android.widget.SimpleAdapter;
  11. public class ListSample extends Activity {
  12. public final static String ITEM_TITLE = "title";
  13. public final static String ITEM_CAPTION = "caption";
  14. public Map<String, ?> createItem(String title, String caption) {
  15. Map<String, String> item = new HashMap<String, String>();
  16. item.put(ITEM_TITLE, title);
  17. item.put(ITEM_CAPTION, caption);
  18. return item;
  19. }
  20. @Override
  21. public void onCreate(Bundle icicle) {
  22. super.onCreate(icicle);
  23. List<Map<String, ?>> security = new LinkedList<Map<String, ?>>();
  24. security.add(createItem("Remember passwords",
  25. "Save usernames and passwords for Web sites"));
  26. security.add(createItem("Clear passwords",
  27. "Save usernames and passwords for Web sites"));
  28. security.add(createItem("Show security warnings",
  29. "Show warning if there is a problem with a site's security"));
  30. // create our list and custom adapter
  31. SeparatedListAdapter adapter = new SeparatedListAdapter(this);
  32. adapter.addSection("Array test", new ArrayAdapter<String>(this,
  33. R.layout.list_item, new String[] { "First item", "Item two" }));
  34. adapter.addSection("Security", new SimpleAdapter(this, security,
  35. R.layout.list_complex,
  36. new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] {
  37. R.id.list_complex_title, R.id.list_complex_caption }));
  38. ListView list = new ListView(this);
  39. list.setAdapter(adapter);
  40. this.setContentView(list);
  41. }
  42. }

这样java代码就写完了。。

最后把xml布局文件粘贴一下就可以跑起来了

[html] view
plain
copy

  1. <!-- list_complex.xml -->
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="vertical"
  7. android:paddingTop="10dip"
  8. android:paddingBottom="10dip"
  9. android:paddingLeft="15dip"
  10. >
  11. <TextView
  12. android:id="@+id/list_complex_title"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:textAppearance="?android:attr/textAppearanceLarge"
  16. />
  17. <TextView
  18. android:id="@+id/list_complex_caption"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:textAppearance="?android:attr/textAppearanceSmall"
  22. />
  23. </LinearLayout>
[html] view
plain
copy

  1. <!-- list_header.xml -->
  2. <TextView
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/list_header_title"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:paddingTop="2dip"
  8. android:paddingBottom="2dip"
  9. android:paddingLeft="5dip"
  10. style="?android:attr/listSeparatorTextViewStyle" />
[html] view
plain
copy

  1. <!-- list_item.xml -->
  2. <TextView
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/list_item_title"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:paddingTop="10dip"
  8. android:paddingBottom="10dip"
  9. android:paddingLeft="15dip"
  10. android:textAppearance="?android:attr/textAppearanceLarge"
  11. />

android中的Section ListView的更多相关文章

  1. Android中动态更新ListView(转)

    在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中.实现步骤:调用ListView的setOnSc ...

  2. android中ScrollView嵌套ListView或GridView显示位置问题

    Android中ScrollView中嵌套ListView或GridView时在开始进入界面时总是显示中间位置,开头的位置显示不出来.这种情况下只需要在ScrollView的父控件中添加以下两行代码即 ...

  3. Android中一个关于ListView的奇怪问题

    今天在做项目的时候发现了一个比较奇怪的问题,是关于ListView的,即ListView的android:height属性会影响程序中ListView的getView()方法的调用次数,如果设置Lis ...

  4. Android中监听ListView滑动到底部

    Android中的应用就是ListView中向下滑动加载更多的功能,不要再onScroll方法中进行判断,那样当滑动到底部的时候,触摸屏幕就会又去加载更多,效果很差,可以自行测试一下: listvie ...

  5. android中ProgressBar和ListView

    ProgressBar进度条的使用情况: 进度条的.xml声明:如果不声明格式,则默认格式为转圆圈的形式,声明进度条的visibility为不可见. <ProgressBar android:i ...

  6. Android中取消GridView & ListView默认的点击背景色

    方法一: gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelector(new ColorDrawa ...

  7. Android中GridView、ListView 的 getChildAt() 方法返回null 问题

    开发的Android app用到了GridView或者ListView,通常使用getChildAt(int position)方法获取当前点击或者选中的View(即position对应的View). ...

  8. Android中如何使用Listview

    第一步 首先在xml文件中声明一个List View控件,并且标明id (这一步其实不用说,怕自学Android的小白不懂,就好比当初的我,哈哈) <?xml version="1.0 ...

  9. Android中动态改变Listview中字体的颜色

    效果如下: 账目显示用的是Listview,要实现的功能为使其根据所在Item是“收入”还是“支出”来把数字设置成绿色或红色 方法是自定义适配器,并重写其中getView()函数,实现如下: //自定 ...

随机推荐

  1. python学习之掷骰子游戏

    """ 通过学习的python知识,写一个简单的python小游戏 游戏名字:掷骰子比大小 游戏规则: 1.玩家可以选择玩掷几个骰子游戏(默认3个) 2.玩家可以设置双方 ...

  2. win10下PLSQL Developer 连接ubuntu上安装的oracle 11g

    说明:过程记录的不是很相信,只记录基本步骤.并不适合想一步一步照做的同学. win10下需要的操作 1.微软官网下载instantclient,然后接到到本地一个文件夹,注意路径不要又空格,中文和括号 ...

  3. JavaScript指定日期格式化

    formatDataToString:function (dates, formats) { var o = { "M+": dates.getMonth() + 1, //月份 ...

  4. flutter packages get 慢 解决方案

    国内使用 flutter packages get 命令,一直是  This is taking an unexpectedly long time 状态 科.学.上.网.无.效. windows解决 ...

  5. XML文件解析之SAX解析

    使用DOM解析的时候是需要把文档的所有内容读入内存然后建立一个DOM树结构,然后通过DOM提供的接口来实现XML文件的解析,如果文件比较小的时候肯定是很方便的.但是如果是XML文件很大的话,那么这种方 ...

  6. 解决sqoop抽数报错:IO Error: Connection reset

    遇到的问题:进行sqoop抽数时,虽然能成功执行,但是过程中有很多这样的信息 19/11/20 15:17:11 INFO mapreduce.Job: Task Id : attempt_15737 ...

  7. 笔记本电脑安装jupyterthemes

    上午准备在老笔记本上也装上jupyter themes,竟然遇到一堆问题: 首先直接 pip install jupyterthemes 参考:https://blog.csdn.net/Jinlon ...

  8. layui模块化使用

    layui模块化使用分为两部分: 1.自身模块的设置与使用.2.自定义模块的设置与使用.直接上案例吧 <a>加载入口模块与自定义模块设置: <b>自定义模块名 <c> ...

  9. vscode开发

    基于 Electron 开发.typescript编写.底层 Node.js 打造的一个编辑器 , 不是IDE,被称为“披着IDE外衣的编辑器”

  10. 前端与SQL

    转载自:http://developer.51cto.com/art/201706/542163.htm