1. 引言
在Android开发过程中往往有这样的需求,将ListView中的内容按年,月,日进行分类显示,要实现这样的效果我们可能有很多种方法,
如:多ListView拼合,自定义ListView组件等,下面介绍一种比较简单,而且实现结构清晰的实现方式,效果图及实现如下。
2. 效果图
3. 功能实现
(1) 主布局(main.xml)实现:
- <?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>
(2) 主Activity实现:
- 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实现:
- 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);
- }
(4) 分类ValueBean实现:
- 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;
- }
- }
(5) 分类Title实现:
- <?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"
- />