Android ListView列表分组

运行效果:


main.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.   android:orientation="horizontal" android:layout_width="fill_parent"  
  6.   
  7.   android:layout_height="fill_parent">  
  8.   
  9.   <ListView android:layout_width="fill_parent"  
  10.   
  11.    android:layout_height="wrap_content" android:id="@+id/MyListView">  
  12.   
  13.   </ListView>  
  14.   
  15. </LinearLayout>  

list_items.xml

[java]  view plain copy
  1.    
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4.   
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  6.   
  7.   android:orientation="vertical" android:layout_width="fill_parent"  
  8.   
  9.   android:layout_height="wrap_content" android:id="@+id/myListItem"  
  10.   
  11.   android:paddingBottom="4dip" android:paddingLeft="12dip"  
  12.   
  13.   android:paddingRight="12dip">  
  14.   
  15.   <TextView android:layout_height="wrap_content"  
  16.   
  17.    android:layout_width="fill_parent" android:id="@+id/itemTitle"  
  18.   
  19.    android:textSize="20dip" />  
  20.   
  21. </LinearLayout>  

list_items_tag.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.   android:layout_width="fill_parent" android:layout_height="wrap_content"  
  6.   
  7.   android:background="#555555" android:paddingLeft="10dip">  
  8.   
  9.   <TextView android:id="@+id/itemTitle" android:layout_width="wrap_content"  
  10.   
  11.    android:layout_height="20dip" android:textColor="#ffffff"  
  12.   
  13.    android:gravity="center_vertical" />  
  14.   
  15. </LinearLayout>  

MyAdapter.java

[java]  view plain copy
  1. package com.iaiai;  
  2.   
  3.    
  4.   
  5. import java.util.List;  
  6.   
  7. import java.util.Map;  
  8.   
  9.    
  10.   
  11. import android.content.Context;  
  12.   
  13. import android.view.LayoutInflater;  
  14.   
  15. import android.view.View;  
  16.   
  17. import android.view.ViewGroup;  
  18.   
  19. import android.widget.BaseAdapter;  
  20.   
  21. import android.widget.TextView;  
  22.   
  23.    
  24.   
  25. public class MyAdapter extends BaseAdapter {  
  26.   
  27.    
  28.   
  29.   private LayoutInflater mInflater;  
  30.   
  31.    
  32.   
  33.   private List<Map<String, String>> listData;  
  34.   
  35.    
  36.   
  37.   private List<Map<String, String>> splitData;  
  38.   
  39.    
  40.   
  41.   public MyAdapter(Context context,  
  42.   
  43.      List<Map<String, String>> listData,  
  44.   
  45.      List<Map<String, String>> splitData) {  
  46.   
  47.    this.mInflater = LayoutInflater.from(context);  
  48.   
  49.    this.listData = listData;  
  50.   
  51.    this.splitData = splitData;  
  52.   
  53.   }  
  54.   
  55.    
  56.   
  57.   @Override  
  58.   
  59.   public int getCount() {  
  60.   
  61.    return listData.size();  
  62.   
  63.   }  
  64.   
  65.    
  66.   
  67.   @Override  
  68.   
  69.   public Object getItem(int position) {  
  70.   
  71.    return listData.get(position);  
  72.   
  73.   }  
  74.   
  75.    
  76.   
  77.   @Override  
  78.   
  79.   public long getItemId(int position) {  
  80.   
  81.    return position;  
  82.   
  83.   }  
  84.   
  85.    
  86.   
  87.   @Override  
  88.   
  89.   public boolean isEnabled(int position) {  
  90.   
  91.    if (splitData.contains(listData.get(position))) {  
  92.   
  93.      return false;  
  94.   
  95.    }  
  96.   
  97.    return super.isEnabled(position);  
  98.   
  99.   }  
  100.   
  101.    
  102.   
  103.   @Override  
  104.   
  105.   public View getView(final int position, View convertView, ViewGroup parent) {  
  106.   
  107.    if (splitData.contains(listData.get(position))) {  
  108.   
  109.      convertView = mInflater.inflate(R.layout.list_items_tag, null);  
  110.   
  111.    } else {  
  112.   
  113.      convertView = mInflater.inflate(R.layout.list_items, null);  
  114.   
  115.    }  
  116.   
  117.    
  118.   
  119.    TextView textView = (TextView) convertView.findViewById(R.id.itemTitle);  
  120.   
  121.    textView.setText(listData.get(position).get("itemTitle"));  
  122.   
  123.    
  124.   
  125.    return convertView;  
  126.   
  127.   }  
  128. }  

MainActivity.java

[java]  view plain copy
  1. package com.iaiai;  
  2.   
  3.    
  4.   
  5. import java.util.ArrayList;  
  6.   
  7. import java.util.HashMap;  
  8.   
  9. import java.util.List;  
  10.   
  11. import java.util.Map;  
  12.   
  13.    
  14.   
  15. import android.app.Activity;  
  16.   
  17. import android.os.Bundle;  
  18.   
  19. import android.widget.ListView;  
  20.   
  21.    
  22.   
  23. public class MainActivity extends Activity {  
  24.   
  25.    
  26.   
  27.   private List<Map<String, String>> mylist = new ArrayList<Map<String, String>>();  
  28.   
  29.   private List<Map<String, String>> splitList = new ArrayList<Map<String, String>>();  
  30.   
  31.    
  32.   
  33.   @Override  
  34.   
  35.   protected void onCreate(Bundle savedInstanceState) {  
  36.   
  37.    super.onCreate(savedInstanceState);  
  38.   
  39.    setContentView(R.layout.main);  
  40.   
  41.    
  42.   
  43.    ListView list = (ListView) findViewById(R.id.MyListView);  
  44.   
  45.    
  46.   
  47.    setData(); // 设置数据  
  48.   
  49.    
  50.   
  51.    // 配置适配器  
  52.   
  53.    MyAdapter adapter = new MyAdapter(this, mylist, splitList); // 布局里的控件id  
  54.   
  55.    // 添加并且显示  
  56.   
  57.    list.setAdapter(adapter);  
  58.   
  59.   }  
  60.   
  61.    
  62.   
  63.   private void setData() {  
  64.   
  65.    // 组织数据源  
  66.   
  67.    Map<String, String> mp = new HashMap<String, String>();  
  68.   
  69.    mp.put("itemTitle""A");  
  70.   
  71.    mylist.add(mp);  
  72.   
  73.    splitList.add(mp);  
  74.   
  75.    
  76.   
  77.    for (int i = 0; i < 3; i++) {  
  78.   
  79.      Map<String, String> map = new HashMap<String, String>();  
  80.   
  81.      map.put("itemTitle""文章1-" + i);  
  82.   
  83.      mylist.add(map);  
  84.   
  85.    }  
  86.   
  87.    
  88.   
  89.    mp = new HashMap<String, String>();  
  90.   
  91.    mp.put("itemTitle""B");  
  92.   
  93.    mylist.add(mp);  
  94.   
  95.    splitList.add(mp);  
  96.   
  97.    
  98.   
  99.    for (int i = 0; i < 6; i++) {  
  100.   
  101.      Map<String, String> map = new HashMap<String, String>();  
  102.   
  103.      map.put("itemTitle""文章2-" + i);  
  104.   
  105.      mylist.add(map);  
  106.   
  107.    }  
  108.   
  109.   }  
  110. }  

========================================================================
以上为示例所有代码,以下为说明

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

Java代码  

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

//默认情况,如果这个方法不是分割符,返回true

//分隔符是无选中和无点击事件的

//说白了,你想不想把改position项当做分隔符,想的话就返回false,否则返回true

public boolean isEnabled (int position)


这个方法刚好用来禁用标签项的响应事件。

 

[java]  view plain copy
  1. @Override  
  2.   
  3. public boolean isEnabled(int position) {  
  4.   
  5.   if(splitData.contains(listData.get(position))) {  
  6.   
  7.    returnfalse;  
  8.   
  9.   }  
  10.   
  11.   returnsuper.isEnabled(position);  
  12.   
  13. }  


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


原地址:http://blog.sina.com.cn/s/blog_4ca9ceef0100za7f.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值