前几天,和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. 在ASP.NET Core中实现自动注入、批量注入

    我们在使用AddScoped.AddTransient.AddSingleton这类方法的时候很是麻烦.我们每增加一个接口以及其实现的时候,是不是需要在这里硬编码注册一行代码呢?项目小还好,但当我们的 ...

  2. putty和psftp命令行参数

    putty和psftp命令行参数 https://the.earth.li/~sgtatham/putty/latest/w32/putty.zip https://the.earth.li/~sgt ...

  3. Project Oberon

    Project Oberon Project Oberon  http://www.projectoberon.com/ Project Oberon 28.11.2018 / 11.12.2018 ...

  4. python2.7.5安装docker-compose的方法

    yum -y install epel-release && yum install -y python-pip && pip install --upgrade pi ...

  5. Go 方法使用

    方法的定义 在 Go 语言里,方法和函数只差了一个,那就是方法在 func 和标识符之间多了一个参数. type user struct { name string, email string, } ...

  6. mount命令解析

    可以参考两位大神的理解 Linux mount 命令 Linux的mount命令详解

  7. [Selenium3+python3.6]自动化测试2-入门

    参考http://www.cnblogs.com/yoyoketang/p/6123890.html #coding=utf-8 #Import webdriver time module from ...

  8. Linux学习笔记(八)Linux常用命令:用户登录查看命令

    一.查看登录用户信息 w [用户名] 二.Who who 三.查询当前登录和过去登陆的用户信息 last 四.查看所有用户最后一次登录时间 lastlog

  9. 【西北大学2019新生赛】序列排序II

    原题: 想了很久,想的是模仿冒泡,从大到小检查每一个数后面的数是否都与它互质,然后把它设为1(等价于放到最后不考虑) 然后一直想数据结垢 出来跟人交流,“这不是挺典型的思维题么哈哈哈” 利用性质: 调 ...

  10. linux配置全局环境变量-jdk

    1.vi /etc/profile 2.输入大写G,定位内容末尾. 3.在末尾输入 export JAVA_HOME=/home/order/soft/jdk PATH=$PATH:$HOME/.lo ...