吸引用户的眼球,是我们至死不渝的追求;
      第一时间呈现最有价值的信息,简明大方,告诉客户,你的选择是多么的明智,这正是你寻觅已久的东西。

分组的应用场合还是很多的,有数据集合的地方往往要分组显示;
      分组的形式也很多,最常见的就是镶嵌在列表中,网上说的很多ExpandListView的也是一种。
      Android自带的通讯录中的联系人是按照拼音首字母(A,B,C,D......)分组分类的,效果如下:
      我们今天也是要实现这样类似的一个效果。

1.样本数据:
      为了突出重点,直击要点,这里提供一个整理好的数据样本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//list:数据集合
private List<String> list = new ArrayList<String>();
//listTag:Tag集合,其中Tag是分类的分割标签,每个分组的header
private List<String> listTag = new ArrayList<String>();
 
public void setData(){
        list.add("A");
        listTag.add("A");
        for(int i=0;i<3;i++){
            list.add("阿凡达"+i);
        }
        list.add("B");
        listTag.add("B");
        for(int i=0;i<3;i++){
            list.add("比特风暴"+i);
        }
        list.add("C");
        listTag.add("C");
        for(int i=0;i<30;i++){
            list.add("查理风云"+i);
        }
}

2.Activity布局准备:
      放置一个listView来呈现数据。
      group_list_activity.xml:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!--简单的列表显示-->
    <ListView android:id="@+id/group_list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:cacheColorHint="#00000000"/>
</LinearLayout>

3.自定义Adapter(本文继承ArrayAdapter):
     这个是本文的重点和核心。 
     Adapter接口为数据和界面搭建了一个访问的桥梁,最重要的就是getView()方法,用这个方法我们可以实现一定程度的界面自定义。
     ArrayAdapter间接实现了Adapter接口,这里我们简单起见,数据源只是提供单一的String数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static class GroupListAdapter extends ArrayAdapter<String>{
    //存放标签的列表,用来判断数据项的类型
    //如果数据项在标签列表中,则是标签项,否则是数据项
    private List<String> listTag = null;
    public GroupListAdapter(Context context, List<String> objects, List<String> tags) {
        super(context, 0, objects);
        this.listTag = tags;
    }
     
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ... ....
    }
}

我们来看看getView方法:

1
2
3
4
//该方法根据adapter的顺序一行一行的组织列表
//其中position表示第几行,也就是当前行在adapter的位置,
//convertView表示第几行的View
View getView(int position, View convertView, ViewGroup parent);

现在我们就是要重写getView方法,来实现列表中嵌入分组标签。
     分组标签也是列表数据项之一,也是被一行一行的画上去的,但是它和其他数据项UI是不一致的,所以我们需要准备2套数据项布局模板:
     数据项模板group_list_item.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip">
    <!-- 图片和文字 -->
    <!-- 随便放了一张图片,稍微美化一下 -->
    <ImageView
       android:src="@drawable/list_icon"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
    <TextView
       android:id="@+id/group_list_item_text"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent"
       android:paddingLeft="5dip"
       android:gravity="center_vertical"/>
</LinearLayout>

标签项模板group_list_item_tag.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 只有文字,但是高度小店,背景色设置为555555灰色 -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#555555"
    android:paddingLeft="10dip">
    <TextView
       android:id="@+id/group_list_item_text"
       android:layout_width="wrap_content"
       android:layout_height="20dip"
       android:textColor="#ffffff"
       android:gravity="center_vertical"/>
</LinearLayout>

好,我们现在把这两个模板应用到getView方法中去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    //根据标签类型加载不通的布局模板
    if(listTag.contains(getItem(position))){
        //如果是标签项
        view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null);
    }else{             
        //否则就是数据项了     
        view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item, null);
    }
    //显示名称
    TextView textView = (TextView) view.findViewById(R.id.group_list_item_text);
    textView.setText(getItem(position));
    //返回重写的view
    return view;
}

 4.禁止标签项的响应事件:
      在ArrayAdapter的父类BaseAdapter中提供了isEnable的()方法,我们看看这个方法:

1
2
3
4
//默认情况,如果这个方法不是分割符,返回true
//分隔符是无选中和无点击事件的
//说白了,你想不想把改position项当做分隔符,想的话就返回false,否则返回true
public boolean isEnabled (int position)

这个方法刚好用来禁用标签项的响应事件。具体实现如下:

1
2
3
4
5
6
7
@Override
public boolean isEnabled(int position) {
    if(listTag.contains(getItem(position))){
        return false;
    }
    return super.isEnabled(position);
}

现在标签项不会再有任何触控效果了,犹如一块死木板。

5.完整代码:
      整个Activity和Adapter代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class GroupListActivity extends Activity {
     
    private GroupListAdapter adapter = null;
    private ListView listView = null;
    private List<String> list = new ArrayList<String>();
    private List<String> listTag = new ArrayList<String>();
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_list_activity);
         
        setData();
        adapter = new GroupListAdapter(this, list, listTag);
        listView = (ListView)findViewById(R.id.group_list);
        listView.setAdapter(adapter);
    }
 
    public void setData(){
        list.add("A");
        listTag.add("A");
        for(int i=0;i<3;i++){
            list.add("阿凡达"+i);
        }
        list.add("B");
        listTag.add("B");
        for(int i=0;i<3;i++){
            list.add("比特风暴"+i);
        }
        list.add("C");
        listTag.add("C");
        for(int i=0;i<30;i++){
            list.add("查理风云"+i);
        }
    }
    private static class GroupListAdapter extends ArrayAdapter<String>{
         
        private List<String> listTag = null;
        public GroupListAdapter(Context context, List<String> objects, List<String> tags) {
            super(context, 0, objects);
            this.listTag = tags;
        }
         
        @Override
        public boolean isEnabled(int position) {
            if(listTag.contains(getItem(position))){
                return false;
            }
            return super.isEnabled(position);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if(listTag.contains(getItem(position))){
                view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null);
            }else{                   
                view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item, null);
            }
            TextView textView = (TextView) view.findViewById(R.id.group_list_item_text);
            textView.setText(getItem(position));
            return view;
        }
    }
}

6.最终效果:

App列表之分组ListView的更多相关文章

  1. Android学习系列(9)--App列表之分组ListView

    吸引用户的眼球,是我们至死不渝的追求:      第一时间呈现最有价值的信息,简明大方,告诉客户,你的选择是多么的明智,这正是你寻觅已久的东西.       分组的应用场合还是很多的,有数据集合的地方 ...

  2. Android学习系列(17)--App列表之圆角ListView(续)

    http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html   本来这篇文章想并到上篇Android学习系列(16)- ...

  3. Android学习系列(16)--App列表之圆角ListView

    有些东西看多了,就厌烦了:extjs对我这种感觉最为强烈.甚至,有时觉得设计之殇是审美疲劳.直角看多了,就想看看圆角,不知何时,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,iphone中 ...

  4. Android学习系列(15)--App列表之游标ListView(索引ListView)

    游标ListView,提供索引标签,使用户能够快速定位列表项.      也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧.      一看图啥都懂了: 1. ...

  5. Android学习系列(11)--App列表之拖拽ListView(下)

    接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法.     在这个方法中我们主要是处理 ...

  6. Android学习系列(10)--App列表之拖拽ListView(上)

     研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨.      鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...

  7. Android学习系列--App列表之拖拽ListView(下)

    接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法.     在这个方法中我们主要是处理 ...

  8. Android学习系列--App列表之拖拽ListView(上)

    研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨.      鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. 一 ...

  9. 【Android】12.0 UI开发(三)——列表控件ListView的简单实现2

    1.0 由于书上内容,已经和实际编程的兼容性已经不太友好,重写了项目,用于进一步学习列表控件ListView. 2.0 新建项目ListViewTest,其中文件目录如下: 3.0 ActivityC ...

随机推荐

  1. 【BZOJ3295】【块状链表+树状数组】动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  2. Mysql 目录恢复注意事项

    SET @mycnt=0; SELECT @mycnt := @mycnt +1 as mycnt, a.*, b.* FROM a, b; 表中第一列即为mycnt,从1开始计数. set @num ...

  3. Oracle数据库之视图与索引

    Oracle数据库之视图与索引 1. 视图简介 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改. 视图基于的表称为基表,视图是存储在数据字典里的一条SE ...

  4. 用python选择及显示三级目录,可返回上层目录以及随时跳出。

    # -*- coding: utf-8 -*-"""Created on Fri Jul 29 09:43:38 2016 @author: yinggang" ...

  5. [r]Seven habits of effective text editing

    Seven habits of effective text editing(via) Bram Moolenaar November 2000 If you spend a lot of time ...

  6. 写个自己的Xcode4插件

    推荐:http://onevcat.com/2013/02/xcode-plugin/   刚写iOS程序的时候就知道Xcode支持第三方插件,比如ColorSense等很实用的插件,但Xcode的插 ...

  7. 开发Nginx模块

    开发Nginx模块 前面的哪些话 关于Nginx模块开发的博客资料,网上很多,很多.但是,每篇博客都只提要点,无法"step by step"照着做,对于初次接触Nginx开发的同 ...

  8. java枚举小结

    如何定义一个枚举类? //定义了4个等级 enum Level{ A,B,C,D } 枚举类的实质: class Level{ public static final Level A = new Le ...

  9. Phalcon的学习篇-phalcon和devtools的安装和设置

    A Phalcon在Windows上的安装 1 从Phalcon for Windows下载适合的DLL, 这里的适合 主要看两个方面 1 PHP的版本 2 线程是否是安全 3 编译版本 如果不清楚这 ...

  10. pycharm去掉拼写检查

    http://zhidao.baidu.com/question/523436629.html