分享

Android中ListView分类

 软件团队头目 2012-10-15

Android中ListView分类

分类: Android基础 1209人阅读 评论(31) 收藏 举报

1. 引言

 

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

 

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

 

2. 效果图

    ListView分类

3. 功能实现

 

    (1) 主布局(main.xml)实现:

[java:firstline[1]] view plaincopy?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android = "http://schemas./apk/res/android"  
  3.     android:orientation = "vertical"  
  4.     android:layout_width = "fill_parent"  
  5.     android:layout_height = "fill_parent"  
  6.     >  
  7.     <ListView  
  8.         android:id = "@+id/categoryList"  
  9.         android:layout_width = "fill_parent"   
  10.         android:layout_height = "fill_parent"  
  11.         />  
  12. </LinearLayout>   

 

    (2) 主Activity实现:

[java:firstline[NaN]] view plaincopy?
  1. package com.flora;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.ArrayAdapter;  
  7. import android.widget.ListView;  
  8. import android.widget.TextView;  
  9. public class ListViewCategoryActivity extends Activity {  
  10.       
  11.     private String [] mContacts = {"马英才""张三""李四"};  
  12.     private String [] mMusic = {"素顔""庐州月""半城烟沙"};  
  13.     private String [] mEBook = {"拆掉思维里的墙""淡定力""人脉决定命脉"};  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         mCategoryAdapter.addCategory("人名"new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mContacts));  
  21.           
  22.         mCategoryAdapter.addCategory("音乐",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mMusic));  
  23.           
  24.         mCategoryAdapter.addCategory("书籍",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mEBook));  
  25.           
  26.         ListView categoryList = (ListView) findViewById(R.id.categoryList);  
  27.           
  28.         categoryList.setAdapter(mCategoryAdapter);  
  29.     }  
  30.       
  31.     private CategoryAdapter mCategoryAdapter = new CategoryAdapter() {  
  32.         @Override  
  33.         protected View getTitleView(String title, int index, View convertView, ViewGroup parent) {  
  34.             TextView titleView;  
  35.               
  36.             if (convertView == null) {  
  37.                 titleView = (TextView)getLayoutInflater().inflate(R.layout.title, null);  
  38.             } else {  
  39.                 titleView = (TextView)convertView;  
  40.             }  
  41.               
  42.             titleView.setText(title);  
  43.               
  44.             return titleView;  
  45.         }  
  46.     };  
  47.       
  48. }  

 

    (3) Adapter实现:

[java:firstline[NaN]] view plaincopy?
  1. package com.flora;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.Adapter;  
  7. import android.widget.BaseAdapter;  
  8. public abstract class CategoryAdapter extends BaseAdapter {  
  9.     private List<Category> categories = new ArrayList<Category>();  
  10.       
  11.     public void addCategory(String title, Adapter adapter) {  
  12.         categories.add(new Category(title, adapter));  
  13.     }  
  14.       
  15.     @Override  
  16.     public int getCount() {  
  17.         int total = 0;  
  18.           
  19.         for (Category category : categories) {  
  20.             total += category.getAdapter().getCount() + 1;  
  21.         }  
  22.           
  23.         return total;  
  24.     }  
  25.     @Override  
  26.     public Object getItem(int position) {  
  27.         for (Category category : categories) {  
  28.             if (position == 0) {  
  29.                 return category;  
  30.             }  
  31.               
  32.             int size = category.getAdapter().getCount() + 1;  
  33.             if (position < size) {  
  34.                 return category.getAdapter().getItem(position-1);  
  35.             }  
  36.             position -= size;  
  37.         }  
  38.           
  39.         return null;  
  40.     }  
  41.     @Override  
  42.     public long getItemId(int position) {  
  43.         return position;  
  44.     }  
  45.       
  46.     public int getViewTypeCount() {  
  47.         int total = 1;  
  48.           
  49.         for (Category category : categories) {  
  50.             total += category.getAdapter().getViewTypeCount();  
  51.         }  
  52.           
  53.         return total;  
  54.     }  
  55.     public int getItemViewType(int position) {  
  56.         int typeOffset = 1;  
  57.           
  58.         for (Category category : categories) {  
  59.             if (position == 0) {  
  60.                 return 0;  
  61.             }  
  62.               
  63.             int size = category.getAdapter().getCount() + 1;  
  64.             if (position < size) {  
  65.                 return typeOffset + category.getAdapter().getItemViewType(position - 1);  
  66.             }  
  67.             position -= size;  
  68.               
  69.             typeOffset += category.getAdapter().getViewTypeCount();  
  70.         }  
  71.           
  72.         return -1;  
  73.     }  
  74.     @Override  
  75.     public View getView(int position, View convertView, ViewGroup parent) {  
  76.         int categoryIndex = 0;  
  77.           
  78.         for (Category category : categories) {  
  79.             if (position == 0) {  
  80.                 return getTitleView(category.getTitle(), categoryIndex,convertView, parent);  
  81.             }  
  82.             int size = category.getAdapter().getCount()+1;  
  83.             if (position < size) {  
  84.                 return category.getAdapter().getView(position - 1, convertView, parent);  
  85.             }  
  86.             position -= size;  
  87.               
  88.             categoryIndex++;  
  89.         }  
  90.           
  91.         return null;  
  92.     }  
  93.       
  94.     protected abstract View getTitleView(String caption,int index,View convertView,ViewGroup parent);  
  95.       
  96. }   

 

    (4) 分类ValueBean实现:

[java:firstline[NaN]] view plaincopy?
  1. package com.flora;  
  2. import android.widget.Adapter;  
  3. public class Category {  
  4.     private String mTitle;  
  5.       
  6.     private Adapter mAdapter;  
  7.     public Category(String title, Adapter adapter) {  
  8.         mTitle = title;  
  9.         mAdapter = adapter;  
  10.     }  
  11.       
  12.     public void setTile(String title) {  
  13.         mTitle = title;  
  14.     }  
  15.       
  16.     public String getTitle() {  
  17.         return mTitle;  
  18.     }  
  19.       
  20.     public void setAdapter(Adapter adapter) {  
  21.         mAdapter = adapter;  
  22.     }  
  23.       
  24.     public Adapter getAdapter() {  
  25.         return mAdapter;  
  26.     }  
  27.       
  28. }   

 

    (5) 分类Title实现:

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

 



--------------------滑动时变黑的问题----------------------------



老问题,Google一下就能找到N多答案,为方便自己日后查阅,记录如下:

 

手指在ListView上下滚动时,ListViewItem背景变黑,因为在滚动的时候为了提升性能做了优化,为提高滚动的性能,Android框架在ListView中引入CacheColorHint属性。如果该值为非0,则说明该ListView绘制在单色不透明的背景上,在默认情况下该值为#191919,也就是黑色主题中的黑色背景颜色值,这样当ListView滚动的时候就会使用该值来绘制ListView的背景。

 

两种解决办法:

1、xml中,ListView内新增如下属性:

 

Xml代码 Xml代码   收藏代码
  1. android:cacheColorHint="#00000000"  

 

2、Java类文件中,对ListView设置如下属性:

Java代码   收藏代码
  1. listview.setCacheColorHint(Color.TRANSPARENT);  

 

OK,完美解决呵呵 


  1. android:cacheColorHint="#00000000"  

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多