前几天,和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. Linux 配置jdk vim和 Linux 基本操作

    1下载jdk tar.gz 安装包(http://www.oracle.com/) 注意安装机器的Linux 是x86(32位)还是x64(64位)的 2使用tar -zxvf jdk.tar.gz解 ...

  2. C# List<> Find相关接口学习

    参考 http://blog.csdn.net/daigualu/article/details/54315564 示例: List<int> test = new List<int ...

  3. vue cli3 打包到tomcat上报错问题

    首先  项目打包步骤 1.vue config.js  添加 publicPath: './', // 公共路径 assetsDir:'static', 2.将代理注释掉 proxy 3.将hash需 ...

  4. mycat 报错 java.lang.OutOfMemoryError: Java heap space

    今天排查mysql的错误日志发现  wrapper.log  中有如下错误日志 INFO   | jvm 1    | 2019/10/20 12:52:31 | java.lang.OutOfMem ...

  5. JS与JQuery的一些对比

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

  6. 批量删除checkbox前台后台

    <%@ page contentType="text/html;charset=UTF-8" %><%@ include file="/WEB-INF/ ...

  7. 二〇一八-美团工程师面试解析(Java)

    一轮面试: 小数是怎么存的 算法题:N二进制有多少个1 Linux命令(不熟悉 JVM垃圾回收算法 C或者伪代码实现复制算法 volatile 树的先序中序后序以及应用场景 Mysql存储记录的数据结 ...

  8. apidoc 工具的使用

    使用rest framerok时,需要写API接口文档,此时就需要用到 apidoc(个人觉得这个用的比较顺手) 需要安装nodejs,,, windows 下 1 然后验证是否安装成功  node ...

  9. Tornado目录

    第一篇:白话tornado源码之一个脚本引发的血案 第二篇:白话tornado源码之待请求阶段 第三篇:白话tornado源码之请求来了 第四篇:白话tornado源码之褪去模板外衣的前戏 第五篇:白 ...

  10. vba代码阅读

    #If Vba7 Then #如果是运行在64位office上 Declare PtrSafe Sub...#Else #如果是运行在32位office上 Declare Sub...#EndIf 在 ...