前几天,和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. .net core 依赖注入在特性中的应用

    .net core 依赖注入在特性中的应用,不知道怎么用属性注入,那么在特性中的构造函数里,怎么用接口的方法呢? 来一个简单的例子: 主要思路是把ServiceProvider 静态全局化: publ ...

  2. koa2中间件学习笔记

    洋葱模型 整个洋葱就是服务端程序app,每层洋葱皮都是一个中间件,传入requrest,经过各个中间件处理之后传出response. 新建中间件m1.js,m2.js koa-learn/middle ...

  3. session过期,登录页面嵌套问题解决

    项目主页是框架模式时,如果登录后长时间没有活动(操作),存储在session中的登录信息过期了,这时再去进行操作时,就会出现登录页面嵌套的问题,怎么解决呢? 这里介绍一种方法,只需要加上一段javas ...

  4. oracle exists和 not exists 的用法

    比如 a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在: select * from a where exists(select 1 from b where b ...

  5. MySQL之系系统信息函数

    1.VERSION() 用法:返回MySQL服务器的版本 举例: mysql> select VERSION(); +-----------+ | VERSION() | +---------- ...

  6. 我用Python爬虫挣钱的那点事

    在下写了10年Python,期间写了各种奇葩爬虫,挣各种奇葩的钱,写这篇文章总结下几种爬虫挣钱的方式. 1.最典型的就是找爬虫外包活儿.这个真是体力活,最早是在国外各个freelancer网站上找适合 ...

  7. AtCoder Beginner Contest 133 F Colorful Tree

    Colorful Tree 思路: 如果强制在线的化可以用树链剖分. 但这道题不强制在线,那么就可以将询问进行差分,最后dfs时再计算每个答案的修改值, 只要维护两个数组就可以了,分别表示根节点到当前 ...

  8. Qtxlsx

    https://blog.csdn.net/qq_40194498/article/details/80817264

  9. java堆栈信息查看,以及JVM性能查看工具-jconsole+jmap

    java-core  P487 P515 chapter11,主要讲java的异常,里面很多内容收获良多,之前一直没注意过的. 一,Throwable类获得StackTraceElement ,可进行 ...

  10. tomcat——context.xml

    本机tomcat位置:D:\tomcat7\apache-tomcat-7.0.61 context.xml 位置:D:\tomcat7\apache-tomcat-7.0.61\conf 每个Web ...