Android中ListView分类

1. 引言

 

    在Android开发过程中往往有这样的需求,将ListView中的内容按年,月,日进行分类显示,要实现这样的效果我们可能有很多种方法,

 

    如:多ListView拼合,自定义ListView组件等,下面介绍一种比较简单,而且实现结构清晰的实现方式,效果图及实现如下。

 

2. 效果图

    ListView分类

3. 功能实现

 

    (1) 主布局(main.xml)实现:

[java] view plaincopy

  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.     <ListView  

  8.         android:id = "@+id/categoryList"  

  9.         android:layout_width = "fill_parent"   

  10.         android:layout_height = "fill_parent"  

  11.         />  

  12. </LinearLayout>   

 

    (2) 主Activity实现:

[java] view plaincopy

  1. package com.flora;  

  2. import android.app.Activity;  

  3. import android.os.Bundle;  

  4. import android.view.View;  

  5. import android.view.ViewGroup;  

  6. import android.widget.ArrayAdapter;  

  7. import android.widget.ListView;  

  8. import android.widget.TextView;  

  9. public class ListViewCategoryActivity extends Activity {  

  10.       

  11.     private String [] mContacts = {"马英才""张三""李四"};  

  12.     private String [] mMusic = {"素顔""庐州月""半城烟沙"};  

  13.     private String [] mEBook = {"拆掉思维里的墙""淡定力""人脉决定命脉"};  

  14.       

  15.     @Override  

  16.     public void onCreate(Bundle savedInstanceState) {  

  17.         super.onCreate(savedInstanceState);  

  18.         setContentView(R.layout.main);  

  19.           

  20.         mCategoryAdapter.addCategory("人名"new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mContacts));  

  21.           

  22.         mCategoryAdapter.addCategory("音乐",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMusic));  

  23.           

  24.         mCategoryAdapter.addCategory("书籍",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mEBook));  

  25.           

  26.         ListView categoryList = (ListView) findViewById(R.id.categoryList);  

  27.           

  28.         categoryList.setAdapter(mCategoryAdapter);  

  29.     }  

  30.       

  31.     private CategoryAdapter mCategoryAdapter = new CategoryAdapter() {  

  32.         @Override  

  33.         protected View getTitleView(String title, int index, View convertView, ViewGroup parent) {  

  34.             TextView titleView;  

  35.               

  36.             if (convertView == null) {  

  37.                 titleView = (TextView)getLayoutInflater().inflate(R.layout.title, null);  

  38.             } else {  

  39.                 titleView = (TextView)convertView;  

  40.             }  

  41.               

  42.             titleView.setText(title);  

  43.               

  44.             return titleView;  

  45.         }  

  46.     };  

  47.       

  48. }  

 

    (3) Adapter实现:

[java] view plaincopy

  1. package com.flora;  

  2. import java.util.ArrayList;  

  3. import java.util.List;  

  4. import android.view.View;  

  5. import android.view.ViewGroup;  

  6. import android.widget.Adapter;  

  7. import android.widget.BaseAdapter;  

  8. public abstract class CategoryAdapter extends BaseAdapter {  

  9.     private List<Category> categories = new ArrayList<Category>();  

  10.       

  11.     public void addCategory(String title, Adapter adapter) {  

  12.         categories.add(new Category(title, adapter));  

  13.     }  

  14.       

  15.     @Override  

  16.     public int getCount() {  

  17.         int total = 0;  

  18.           

  19.         for (Category category : categories) {  

  20.             total += category.getAdapter().getCount() + 1;  

  21.         }  

  22.           

  23.         return total;  

  24.     }  

  25.     @Override  

  26.     public Object getItem(int position) {  

  27.         for (Category category : categories) {  

  28.             if (position == 0) {  

  29.                 return category;  

  30.             }  

  31.               

  32.             int size = category.getAdapter().getCount() + 1;  

  33.             if (position < size) {  

  34.                 return category.getAdapter().getItem(position-1);  

  35.             }  

  36.             position -= size;  

  37.         }  

  38.           

  39.         return null;  

  40.     }  

  41.     @Override  

  42.     public long getItemId(int position) {  

  43.         return position;  

  44.     }  

  45.       

  46.     public int getViewTypeCount() {  

  47.         int total = 1;  

  48.           

  49.         for (Category category : categories) {  

  50.             total += category.getAdapter().getViewTypeCount();  

  51.         }  

  52.           

  53.         return total;  

  54.     }  

  55.     public int getItemViewType(int position) {  

  56.         int typeOffset = 1;  

  57.           

  58.         for (Category category : categories) {  

  59.             if (position == 0) {  

  60.                 return 0;  

  61.             }  

  62.               

  63.             int size = category.getAdapter().getCount() + 1;  

  64.             if (position < size) {  

  65.                 return typeOffset + category.getAdapter().getItemViewType(position - 1);  

  66.             }  

  67.             position -= size;  

  68.               

  69.             typeOffset += category.getAdapter().getViewTypeCount();  

  70.         }  

  71.           

  72.         return -1;  

  73.     }  

  74.     @Override  

  75.     public View getView(int position, View convertView, ViewGroup parent) {  

  76.         int categoryIndex = 0;  

  77.           

  78.         for (Category category : categories) {  

  79.             if (position == 0) {  

  80.                 return getTitleView(category.getTitle(), categoryIndex,convertView, parent);  

  81.             }  

  82.             int size = category.getAdapter().getCount()+1;  

  83.             if (position < size) {  

  84.                 return category.getAdapter().getView(position - 1, convertView, parent);  

  85.             }  

  86.             position -= size;  

  87.               

  88.             categoryIndex++;  

  89.         }  

  90.           

  91.         return null;  

  92.     }  

  93.       

  94.     protected abstract View getTitleView(String caption,int index,View convertView,ViewGroup parent);  

  95.       

  96. }   

 

    (4) 分类ValueBean实现:

[java] view plaincopy

  1. package com.flora;  

  2. import android.widget.Adapter;  

  3. public class Category {  

  4.     private String mTitle;  

  5.       

  6.     private Adapter mAdapter;  

  7.     public Category(String title, Adapter adapter) {  

  8.         mTitle = title;  

  9.         mAdapter = adapter;  

  10.     }  

  11.       

  12.     public void setTile(String title) {  

  13.         mTitle = title;  

  14.     }  

  15.       

  16.     public String getTitle() {  

  17.         return mTitle;  

  18.     }  

  19.       

  20.     public void setAdapter(Adapter adapter) {  

  21.         mAdapter = adapter;  

  22.     }  

  23.       

  24.     public Adapter getAdapter() {  

  25.         return mAdapter;  

  26.     }  

  27.       

  28. }   

 

    (5) 分类Title实现:

[java] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <TextView  

  3.     xmlns:android = "http://schemas.android.com/apk/res/android"  

  4.     android:layout_width = "match_parent"  

  5.     android:layout_height = "match_parent"  

  6.     android:minHeight = "30dip"  

  7.     android:gravity = "center_vertical"  

  8.     android:paddingLeft = "10dip"  

  9.     android:background = "@color/title_background_color"  

  10.     />   


转载于:https://my.oschina.net/yuanxulong/blog/532888

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值