主要是实现ListView的getItemViewType和getViewTypeCount两个方法,对于分类本身也占用一个item。
package com.example.categorylistview.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.categorylistview.R;
import com.example.categorylistview.bean.ImageCategory;
import com.example.categorylistview.bean.ImageCategory.Image;
public class CategoryAdapter extends BaseAdapter{
private static final int TYPE_CATEGORY_ITEM = 0;
private static final int TYPE_ITEM = 1;
private List mListData;
private LayoutInflater mInflater;
public CategoryAdapter(Context context, List pData) {
mListData = pData;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
int count = 0;
if (null != mListData) {
// 所有分类中item的总和是ListVIew Item的总个数
// for (ImageCategory category : mListData) {
// count += category.
// }
count = mListData.size();
}
return count;
}
@Override
public Object getItem(int position) {
// 同一分类内,第一个元素的索引值
int categroyFirstIndex = 0;
for (ImageCategory category : mListData) {
int size = category.getItemCount();
// 在当前分类中的索引值
int categoryIndex = position - categroyFirstIndex;
// item在当前分类内
if (categoryIndex < size) {
return category.getItem(categoryIndex);
}
// 索引移动到当前分类结尾,即下一个分类第一个元素索引
categroyFirstIndex += size;
}
return null;
}
public Object getItemName(int position) {
// 同一分类内,第一个元素的索引值
int categroyFirstIndex = 0;
for (ImageCategory category : mListData) {
int size = category.getItemCount();
// 在当前分类中的索引值
int categoryIndex = position - categroyFirstIndex;
// item在当前分类内
if (categoryIndex < size) {
return category.categoryName;
}
categroyFirstIndex += size;
}
return null;
}
@Override
public int getItemViewType(int position) {
int categroyFirstIndex = 0;
for (ImageCategory category : mListData) {
int size = category.getItemCount();
// 在当前分类中的索引值
int categoryIndex = position - categroyFirstIndex;
if (categoryIndex == 0) {
return TYPE_CATEGORY_ITEM;
}
categroyFirstIndex += size;
}
return TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int itemViewType = getItemViewType(position);
CategoryHolder categoryHolder;
ViewHolder viewHolder = null;
switch (itemViewType) {
case TYPE_CATEGORY_ITEM:
if (null == convertView) {
categoryHolder = new CategoryHolder();
convertView = mInflater.inflate(R.layout.category, null);
categoryHolder.tv_category_title = (TextView) convertView.findViewById(R.id.tv_category_title);
convertView.setTag(categoryHolder);
} else {
categoryHolder = (CategoryHolder) convertView.getTag();
}
String itemName = (String) getItemName(position);
categoryHolder.tv_category_title.setText(""+itemName);
break;
case TYPE_ITEM:
if (null == convertView) {
convertView = mInflater.inflate(R.layout.activity_main, null);
viewHolder = new ViewHolder();
viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ImageCategory.Image image = (Image) getItem(position);
String text = image.name+" "+image.size+" "+image.date;
// 绑定数据
viewHolder.tv_title.setText(text);
viewHolder.iv_icon.setImageResource(R.drawable.logo);
break;
}
return convertView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) != TYPE_CATEGORY_ITEM;
}
private class ViewHolder {
TextView tv_title;
ImageView iv_icon;
}
private static class CategoryHolder {
TextView tv_category_title;
}
}
public class ImageCategory {
public String categoryName;
public List images;
public static class Image {
public String name;
public String url;
public String size;
public String date;
public Image(String name, String url, String size, String date) {
super();
this.name = name;
this.url = url;
this.size = size;
this.date = date;
}
}
public Object getItem(int pPosition) {
// Category排在第一位
if (pPosition == 0) {
return this;
} else {
return images.get(pPosition - 1);
}
}
/**
* 当前类别Item总数。Category也需要占用一个Item
*
* @return
*/
public int getItemCount() {
return images.size() + 1;
}
}
public class DataSource {
public static List getCategorys(){
List categorys = new ArrayList();
for (int i = 0; i < 5; i++) {
ImageCategory category = new ImageCategory();
category.categoryName="分类"+i;
List mImages = new ArrayList();
for (int j = 0; j < 5; j++) {
Image image = new Image(category.categoryName+"- 风景图片"+i,"image/path/"+j,"3"
,"2015-"+i+"-2"+j);
mImages.add(image);
}
category.images = mImages;
categorys.add(category);
}
return categorys;
}
}
public class MainActivity extends Activity {
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.lv_data);
mListView.setAdapter(new CategoryAdapter(this, DataSource.getCategorys()));
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
String name = "";
int type = mListView.getAdapter().getItemViewType(position);
if (type ==CategoryAdapter.TYPE_CATEGORY_ITEM) {
ImageCategory category = (ImageCategory) mListView.getAdapter().getItem(position);
name = category.categoryName;
}else if (type ==CategoryAdapter.TYPE_ITEM) {
Image category = (Image) mListView.getAdapter().getItem(position);
name = category.name+" - "+category.size;
}
Toast t = Toast.makeText(getApplicationContext(), name+"", 0);
t.show();
}
});
}
}