Android ListView分类/分组效果 - 第二种实现方式

来源:互联网 发布:cba球员卧推数据 编辑:程序博客网 时间:2024/06/03 05:08

http://blog.csdn.net/androiddevelop/article/details/8316759


之前的文章  《 Android ListView分类/分组效果 - 第一种实现方式》,当前是第二种实现方式。


一、实现ListView分类显示效果,目前我知道的有两种方案:
1. 每一个ItemView都包含用于显示分类信息的view(TitleView)和用于显示内容view。之前实现文章

2. 通过ListView加载不同类型的Item实现。本文属于这种实现方式



二  当前实现描述:

1.  与自定义Adapter的ListVIew雷同,只是多使用两个BaseAdapter API:

[java] view plaincopyprint?
  1. public int getItemViewType (int position)  
  2.   
  3. 获取通过getView为指定项目创建的视图的类型。  
  4. 参数  
  5.     position 在adapter数据里我们想知道视图类型的项目的位置  
  6. 返回值  
  7.     一个整形的视图类型的描述。如果一个视图通过getView(int, View, ViewGroup)方法转换成另一个视图,则两个视图将共享同一类型。注意:整形必须在0和getViewTypeCount()– 1之间。IGNORE_ITEM_VIEW_TYPE也可以返回。  
  8.   
  9.   
  10. public int getViewTypeCount ()  
  11.   
  12. 返回通过getView(int, View, ViewGroup))创建的视图的类型数量。每一个类型表示一组通过getView(int, View, ViewGroup)方法转换过的视图。如果adapter针对所有项目返回相同的视图类型,这个方法返回1。这个方法仅仅当adapter设置在AdapterView时调用。  
  13.   
  14. 返回值  
  15. 通过这个adapter创建的视图类型的数量  


2. 视图 -- 在Adapter.getView中通过getItemViewType获取item类型,当前实现为分类item与普通item两种,根据类型创建不同的VIew对象

3. 数据 -- 分类的数据通过List包含自定义数据对象Category,Category内部包含List用于存储当前Category item数据。

4. 覆写BaseAdapter.areAllItemsEnabled  和 BaseAdapter.isEnabled两个方法,确保分类Item不可点击


三  效果图




四  源码下载与伪码




* Activity

[java] view plaincopyprint?
  1. public class MainActivity extends Activity {  
  2.   
  3.     private CategoryAdapter mCustomBaseAdapter;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         ListView listView = (ListView) findViewById(R.id.listView1);  
  11.           
  12.         // 数据  
  13.         ArrayList<Category> listData = getData();  
  14.           
  15.         mCustomBaseAdapter = new CategoryAdapter(getBaseContext(), listData);  
  16.           
  17.         // 适配器与ListView绑定  
  18.         listView.setAdapter(mCustomBaseAdapter);  
  19.           
  20.         listView.setOnItemClickListener(new ItemClickListener());  
  21.     }  
  22.   
  23.       
  24.     private class ItemClickListener implements OnItemClickListener {  
  25.   
  26.         @Override  
  27.         public void onItemClick(AdapterView<?> parent, View view, int position,  
  28.                 long id) {  
  29.             Toast.makeText(getBaseContext(),  (String)mCustomBaseAdapter.getItem(position),  
  30.                     Toast.LENGTH_SHORT).show();  
  31.         }  
  32.           
  33.     }  
  34.       
  35.       
  36.     /** 
  37.      * 创建测试数据 
  38.      */  
  39.     private ArrayList<Category> getData() {  
  40.         ArrayList<Category> listData = new ArrayList<Category>();  
  41.         Category categoryOne = new Category("路人甲");  
  42.         categoryOne.addItem("马三立");  
  43.         categoryOne.addItem("赵本山");  
  44.         categoryOne.addItem("郭德纲");  
  45.         categoryOne.addItem("周立波");  
  46.         Category categoryTwo = new Category("事件乙");  
  47.         categoryTwo.addItem("**贪污");  
  48.         categoryTwo.addItem("**照门");  
  49.         Category categoryThree = new Category("书籍丙");  
  50.         categoryThree.addItem("10天学会***");  
  51.         categoryThree.addItem("**大全");  
  52.         categoryThree.addItem("**秘籍");  
  53.         categoryThree.addItem("**宝典");  
  54.         categoryThree.addItem("10天学会***");  
  55.         categoryThree.addItem("10天学会***");  
  56.         categoryThree.addItem("10天学会***");  
  57.         categoryThree.addItem("10天学会***");  
  58.         Category categoryFour = new Category("书籍丙");  
  59.         categoryFour.addItem("河南");  
  60.         categoryFour.addItem("天津");  
  61.         categoryFour.addItem("北京");  
  62.         categoryFour.addItem("上海");  
  63.         categoryFour.addItem("广州");  
  64.         categoryFour.addItem("湖北");  
  65.         categoryFour.addItem("重庆");  
  66.         categoryFour.addItem("山东");  
  67.         categoryFour.addItem("陕西");  
  68.           
  69.         listData.add(categoryOne);  
  70.         listData.add(categoryTwo);  
  71.         listData.add(categoryThree);  
  72.         listData.add(categoryFour);  
  73.           
  74.         return listData;  
  75.     }  
  76.   
  77.       
  78.       
  79. }  

* Adapter

[java] view plaincopyprint?
  1. public class CategoryAdapter extends BaseAdapter {  
  2.   
  3.     private static final int TYPE_CATEGORY_ITEM = 0;  
  4.     private static final int TYPE_ITEM = 1;  
  5.       
  6.     private ArrayList<Category> mListData;  
  7.     private LayoutInflater mInflater;  
  8.       
  9.       
  10.     public CategoryAdapter(Context context, ArrayList<Category> pData) {  
  11.         mListData = pData;  
  12.         mInflater = LayoutInflater.from(context);  
  13.     }  
  14.   
  15.     @Override  
  16.     public int getCount() {  
  17.         int count = 0;  
  18.           
  19.         if (null != mListData) {  
  20.             //  所有分类中item的总和是ListVIew  Item的总个数  
  21.             for (Category category : mListData) {  
  22.                 count += category.getItemCount();  
  23.             }  
  24.         }  
  25.           
  26.         return count;  
  27.     }  
  28.   
  29.     @Override  
  30.     public Object getItem(int position) {  
  31.           
  32.         // 异常情况处理  
  33.         if (null == mListData || position <  0|| position > getCount()) {  
  34.             return null;   
  35.         }  
  36.           
  37.         // 同一分类内,第一个元素的索引值  
  38.         int categroyFirstIndex = 0;  
  39.           
  40.         for (Category category : mListData) {  
  41.             int size = category.getItemCount();  
  42.             // 在当前分类中的索引值  
  43.             int categoryIndex = position - categroyFirstIndex;  
  44.             // item在当前分类内  
  45.             if (categoryIndex < size) {  
  46.                 return  category.getItem( categoryIndex );  
  47.             }  
  48.               
  49.             // 索引移动到当前分类结尾,即下一个分类第一个元素索引  
  50.             categroyFirstIndex += size;  
  51.         }  
  52.           
  53.         return null;  
  54.     }  
  55.   
  56.     @Override  
  57.     public int getItemViewType(int position) {  
  58.         // 异常情况处理  
  59.         if (null == mListData || position <  0|| position > getCount()) {  
  60.             return TYPE_ITEM;   
  61.         }  
  62.           
  63.           
  64.         int categroyFirstIndex = 0;  
  65.           
  66.         for (Category category : mListData) {  
  67.             int size = category.getItemCount();  
  68.             // 在当前分类中的索引值  
  69.             int categoryIndex = position - categroyFirstIndex;  
  70.             if (categoryIndex == 0) {  
  71.                 return TYPE_CATEGORY_ITEM;  
  72.             }  
  73.               
  74.             categroyFirstIndex += size;  
  75.         }  
  76.           
  77.         return TYPE_ITEM;  
  78.     }  
  79.       
  80.     @Override  
  81.     public int getViewTypeCount() {  
  82.         return 2;  
  83.     }  
  84.       
  85.     @Override  
  86.     public long getItemId(int position) {  
  87.         return position;  
  88.     }  
  89.   
  90.     @Override  
  91.     public View getView(int position, View convertView, ViewGroup parent) {  
  92.   
  93.         int itemViewType = getItemViewType(position);  
  94.         switch (itemViewType) {  
  95.         case TYPE_CATEGORY_ITEM:  
  96.             if (null == convertView) {  
  97.                 convertView = mInflater.inflate(R.layout.listview_item_header, null);  
  98.             }  
  99.               
  100.             TextView textView = (TextView) convertView.findViewById(R.id.header);  
  101.             String  itemValue = (String) getItem(position);  
  102.             textView.setText( itemValue );  
  103.             break;  
  104.   
  105.         case TYPE_ITEM:  
  106.             ViewHolder viewHolder = null;  
  107.             if (null == convertView) {  
  108.                   
  109.                 convertView = mInflater.inflate(R.layout.listview_item, null);  
  110.                   
  111.                 viewHolder = new ViewHolder();  
  112.                 viewHolder.content = (TextView) convertView.findViewById(R.id.content);  
  113.                 viewHolder.contentIcon = (ImageView) convertView.findViewById(R.id.content_icon);  
  114.                 convertView.setTag(viewHolder);  
  115.             } else {  
  116.                 viewHolder = (ViewHolder) convertView.getTag();  
  117.             }  
  118.               
  119.              // 绑定数据  
  120.             viewHolder.content.setText( (String)getItem(position) );  
  121.             viewHolder.contentIcon.setImageResource(R.drawable.ic_launcher);  
  122.             break;  
  123.         }  
  124.   
  125.         return convertView;  
  126.     }  
  127.   
  128.       
  129.     @Override  
  130.     public boolean areAllItemsEnabled() {  
  131.         return false;  
  132.     }  
  133.       
  134.     @Override  
  135.     public boolean isEnabled(int position) {  
  136.         return getItemViewType(position) != TYPE_CATEGORY_ITEM;  
  137.     }  
  138.       
  139.       
  140.     private class ViewHolder {  
  141.         TextView content;  
  142.         ImageView contentIcon;  
  143.     }  
  144.       
  145. }  



* 数据对象

[java] view plaincopyprint?
  1. public class Category {  
  2.   
  3.     private String mCategoryName;  
  4.     private List<String> mCategoryItem = new ArrayList<String>();  
  5.   
  6.     public Category(String mCategroyName) {  
  7.         mCategoryName = mCategroyName;  
  8.     }  
  9.       
  10.     public String getmCategoryName() {  
  11.         return mCategoryName;  
  12.     }  
  13.   
  14.     public void addItem(String pItemName) {  
  15.         mCategoryItem.add(pItemName);  
  16.     }  
  17.       
  18.     /** 
  19.      *  获取Item内容 
  20.      *  
  21.      * @param pPosition 
  22.      * @return 
  23.      */  
  24.     public String getItem(int pPosition) {  
  25.         // Category排在第一位  
  26.         if (pPosition == 0) {  
  27.             return mCategoryName;  
  28.         } else {  
  29.             return mCategoryItem.get(pPosition - 1);  
  30.         }  
  31.     }  
  32.       
  33.     /** 
  34.      * 当前类别Item总数。Category也需要占用一个Item 
  35.      * @return  
  36.      */  
  37.     public int getItemCount() {  
  38.         return mCategoryItem.size() + 1;  
  39.     }  
  40.       
  41. }  


三、源码下载地址


2013-03-31  更新与第一种方式关联


转载请注明出处:http://blog.csdn.net/love_world_/article/details/8316759



0 0
原创粉丝点击