zoukankan      html  css  js  c++  java
  • Android学习系列(9)--App列表之分组ListView

    吸引用户的眼球,是我们至死不渝的追求;
          第一时间呈现最有价值的信息,简明大方,告诉客户,你的选择是多么的明智,这正是你寻觅已久的东西。
          分组的应用场合还是很多的,有数据集合的地方往往要分组显示;
          分组的形式也很多,最常见的就是镶嵌在列表中,网上说的很多ExpandListView的也是一种。
          Android自带的通讯录中的联系人是按照拼音首字母(A,B,C,D......)分组分类的,效果如下:
    <ignore_js_op>       我们今天也是要实现这样类似的一个效果。
    1.样本数据:
          为了突出重点,直击要点,这里提供一个整理好的数据样本:


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


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

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

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

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

    我们来看看getView方法:

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

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

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

    标签项模板group_list_item_tag.xml:

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

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

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

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

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

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

    1. @Override
    2. public boolean isEnabled(int position) {
    3.     if(listTag.contains(getItem(position))){
    4.         return false;
    5.     }
    6.     return super.isEnabled(position);
    7. }
    复制代码
        现在标签项不会再有任何触控效果了,犹如一块死木板。
    5.完整代码:
          整个Activity和Adapter代码如下:


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


    6.最终效果:
    <ignore_js_op> 



    本文作者:谦虚的天下

  • 相关阅读:
    双向链表循环
    双向链表的删除操作
    双向链表的插入操作
    双向链表的结构
    双向链表的删除操作
    双向链表循环
    OD使用教程17 调试篇17
    OD使用教程17 调试篇17
    双向链表的结构
    独生子女证办理
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/3595941.html
Copyright © 2011-2022 走看看