Android中ListView分类
<?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> 分类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" /> 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; } } 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; } }; } 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); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。