Android:ListView分类

1. 引言

 

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

 

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

 

2. 效果图

    ListView分类

3. 功能实现

 (1) 主布局(main.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/categoryList"
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent"
         />
</LinearLayout>&nbsp;
  (2) 主Activity实现:



?
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
package com.flora;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewCategoryActivity extends Activity {
     
     private String [] mContacts = { "马英才" , "张三" , "李四" };
     private String [] mMusic = { "素顔" , "庐州月" , "半城烟沙" };
     private String [] mEBook = { "拆掉思维里的墙" , "淡定力" , "人脉决定命脉" };
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         
         mCategoryAdapter.addCategory( "人名" , new ArrayAdapter<String>( this , android.R.layout.simple_list_item_1, mContacts));
         
         mCategoryAdapter.addCategory( "音乐" , new ArrayAdapter<String>( this , android.R.layout.simple_list_item_1, mMusic));
         
         mCategoryAdapter.addCategory( "书籍" , new ArrayAdapter<String>( this , android.R.layout.simple_list_item_1, mEBook));
         
         ListView categoryList = (ListView) findViewById(R.id.categoryList);
         
         categoryList.setAdapter(mCategoryAdapter);
     }
     
     private CategoryAdapter mCategoryAdapter = new CategoryAdapter() {
         @Override
         protected View getTitleView(String title, int index, View convertView, ViewGroup parent) {
             TextView titleView;
             
             if (convertView == null ) {
                 titleView = (TextView)getLayoutInflater().inflate(R.layout.title, null );
             } else {
                 titleView = (TextView)convertView;
             }
             
             titleView.setText(title);
             
             return titleView;
         }
     };
     
}
    (3) 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.flora;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
public abstract class CategoryAdapter extends BaseAdapter {
     private List<Category> categories = new ArrayList<Category>();
     
     public void addCategory(String title, Adapter adapter) {
         categories.add( new Category(title, adapter));
     }
     
     @Override
     public int getCount() {
         int total = 0 ;
         
         for (Category category : categories) {
             total += category.getAdapter().getCount() + 1 ;
         }
         
         return total;
     }
     @Override
     public Object getItem( int position) {
         for (Category category : categories) {
             if (position == 0 ) {
                 return category;
             }
             
             int size = category.getAdapter().getCount() + 1 ;
             if (position < size) {
                 return category.getAdapter().getItem(position- 1 );
             }
             position -= size;
         }
         
         return null ;
     }
     @Override
     public long getItemId( int position) {
         return position;
     }
     
     public int getViewTypeCount() {
         int total = 1 ;
         
         for (Category category : categories) {
             total += category.getAdapter().getViewTypeCount();
         }
         
         return total;
     }
     public int getItemViewType( int position) {
         int typeOffset = 1 ;
         
         for (Category category : categories) {
             if (position == 0 ) {
                 return 0 ;
             }
             
             int size = category.getAdapter().getCount() + 1 ;
             if (position < size) {
                 return typeOffset + category.getAdapter().getItemViewType(position - 1 );
             }
             position -= size;
             
             typeOffset += category.getAdapter().getViewTypeCount();
         }
         
         return - 1 ;
     }
     @Override
     public View getView( int position, View convertView, ViewGroup parent) {
         int categoryIndex = 0 ;
         
         for (Category category : categories) {
             if (position == 0 ) {
                 return getTitleView(category.getTitle(), categoryIndex,convertView, parent);
             }
             int size = category.getAdapter().getCount()+ 1 ;
             if (position < size) {
                 return category.getAdapter().getView(position - 1 , convertView, parent);
             }
             position -= size;
             
             categoryIndex++;
         }
         
         return null ;
     }
     
     protected abstract View getTitleView(String caption, int index,View convertView,ViewGroup parent);
     
}&nbsp;

   (4) 分类ValueBean实现:

?
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
package com.flora;
import android.widget.Adapter;
public class Category {
     private String mTitle;
     
     private Adapter mAdapter;
     public Category(String title, Adapter adapter) {
         mTitle = title;
         mAdapter = adapter;
     }
     
     public void setTile(String title) {
         mTitle = title;
     }
     
     public String getTitle() {
         return mTitle;
     }
     
     public void setAdapter(Adapter adapter) {
         mAdapter = adapter;
     }
     
     public Adapter getAdapter() {
         return mAdapter;
     }
     
}&nbsp;
  (5) 分类Title实现:

?
1
2
3
4
5
6
7
8
9
10
<?xml version= "1.0" encoding= "utf-8" ?>
<TextView
     xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:minHeight = "30dip"
     android:gravity = "center_vertical"
     android:paddingLeft = "10dip"
     android:background = "@color/title_background_color"
     />&nbsp;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值