android ExpandableListView可扩展列表

先看一效果图、

列表中要有 图片和文字:


 

 

 

 

所以我们要实现一个自定义的   适配器。

 

介绍一个类:BaseExpandableListAdapter   

一看就知道是 适配器的一个基类了。

 

 

所以我们自定义的适配器要 继承它。

 

除了 完成这个 适配器,还要有两个自定义模板,分别   组和子列表的,单元模板。如下图:


 

 

 

模板布局xml  要放在 layouts  下面。

 

main_tree_group.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="45dp"  
  5.     android:background="@color/white"  
  6.     android:gravity="center"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         xmlns:android="http://schemas.android.com/apk/res/android"  
  11.         android:id="@+id/main_tree_title_id"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginLeft="20dip"  
  15.         android:background="@color/white"  
  16.         android:text="NoData"  
  17.         android:textColor="@color/black"  
  18.         android:textSize="20dp"  
  19.         android:textStyle="bold" />  
  20.   
  21. </LinearLayout>  

 

 

子列表模板文件

main_tree_child.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@color/white"  
  6.     android:gravity="center_vertical"  
  7.     android:orientation="horizontal"  
  8.     android:paddingBottom="5dp"  
  9.     android:paddingLeft="8dp"  
  10.     android:paddingTop="8dp" >  
  11.   
  12.     <ImageView  
  13.         android:id="@+id/mainChildIcoId"  
  14.         android:layout_width="50dp"  
  15.         android:layout_height="50dp"  
  16.         android:src="@drawable/person_icon" />  
  17.   
  18.     <LinearLayout  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="fill_parent"  
  21.         android:orientation="vertical" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/mainChildText1"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_marginLeft="10dp"  
  28.             android:layout_marginTop="5dp"  
  29.             android:background="@color/white"  
  30.             android:gravity="center_vertical"  
  31.             android:text="CNoData"  
  32.             android:textColor="@color/black"  
  33.             android:textSize="16dp" />  
  34.   
  35.         <TextView  
  36.             android:id="@+id/mainChildText2"  
  37.             android:layout_width="fill_parent"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_marginLeft="10dp"  
  40.             android:background="@color/white"  
  41.             android:gravity="center_vertical"  
  42.             android:text="13693668970"  
  43.             android:textColor="@color/black"  
  44.             android:textSize="12dp" />  
  45.     </LinearLayout>  
  46.   
  47. </LinearLayout>  

 

 

 

 

然后写两个对  模板文件的    bean

 

main_tree_group.xml 对应 bean

Java代码   收藏代码
  1. //父单元  
  2. class ExpandableGroupHolder {  
  3.     TextView title;  
  4. }   
 

main_tree_child.xml

 

Java代码   收藏代码
  1. //单元类  
  2. class ExpandableListHolder {  
  3.     TextView nickName;  
  4.     TextView phone;  
  5.     ImageView ioc;  
  6. }   
 

 

现在来实现最重要的关结。  适配器  

MainListExpandableListAdapter.java

 

我这里把 上面两个模板对应java bean 写成 自定义适配器的内部类。

 

 

 

 

 

 

 

Java代码   收藏代码
  1. package com.main.apadter;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import android.content.Context;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseExpandableListAdapter;  
  11. import android.widget.ImageView;  
  12. import android.widget.TextView;  
  13.   
  14. import com.main.R;  
  15.   
  16. public class MainListExpandableListAdapter extends BaseExpandableListAdapter {  
  17.       
  18.     //单元类  
  19.     class ExpandableListHolder {  
  20.         TextView nickName;  
  21.         TextView phone;  
  22.         ImageView ioc;  
  23.     }   
  24.       
  25.     //父单元  
  26.     class ExpandableGroupHolder {  
  27.         TextView title;  
  28.     }   
  29.       
  30.     private List<Map<String, Object>> groupData;//组显示  
  31.     private List<List<Map<String, Object>>> childData;//子列表  
  32.       
  33.     private LayoutInflater mGroupInflater; //用于加载group的布局xml  
  34.     private LayoutInflater mChildInflater; //用于加载listitem的布局xml  
  35.       
  36.     //自宝义构造  
  37.     public MainListExpandableListAdapter(Context context, List<Map<String, Object>> groupData, List<List<Map<String, Object>>> childData) {  
  38.         this.childData=childData;  
  39.         this.groupData=groupData;  
  40.           
  41.         mChildInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  42.         mGroupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  43.     }  
  44.       
  45.     //必须实现 得到子数据  
  46.     @Override  
  47.     public Object getChild(int groupPosition, int j) {  
  48.         return childData.get(groupPosition).get(j);  
  49.     }  
  50.   
  51.     @Override  
  52.     public long getChildId(int groupPosition, int j) {  
  53.         return groupPosition;  
  54.     }  
  55.   
  56.     @Override  
  57.     public int getChildrenCount(int i) {  
  58.         return childData.get(i).size();  
  59.     }  
  60.   
  61.     @Override  
  62.     public Object getGroup(int i) {  
  63.         return groupData.get(i);  
  64.     }  
  65.   
  66.     @Override  
  67.     public int getGroupCount() {  
  68.         return groupData.size();  
  69.     }  
  70.   
  71.     @Override  
  72.     public long getGroupId(int i) {  
  73.         return i;  
  74.     }  
  75.   
  76.     @Override  
  77.     public boolean hasStableIds() {//行是否具有唯一id  
  78.         return false;  
  79.     }  
  80.   
  81.     @Override  
  82.     public boolean isChildSelectable(int i, int j) {//行是否可选  
  83.         return false;  
  84.     }  
  85.       
  86.     @Override  
  87.     public View getGroupView(int groupPosition, boolean flag, View convertView, ViewGroup viewgroup) {  
  88.         ExpandableGroupHolder holder = null//清空临时变量holder  
  89.         if (convertView == null) { //判断view(即view是否已构建好)是否为空  
  90.   
  91.             convertView = mGroupInflater.inflate(R.layout.main_tree_group, null);  
  92.             holder = new ExpandableGroupHolder();  
  93.             holder.title=(TextView) convertView.findViewById(R.id.main_tree_title_id);  
  94.             convertView.setTag(holder);  
  95.         } else { //若view不为空,直接从view的tag属性中获得各子视图的引用  
  96.             holder = (ExpandableGroupHolder) convertView.getTag();  
  97.         }  
  98.         String title=(String)this.groupData.get(groupPosition).get("title");  
  99.         holder.title.setText(title);  
  100.         notifyDataSetChanged();  
  101.         return convertView;  
  102.     }  
  103.       
  104.     @Override  
  105.     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,  
  106.             ViewGroup viewgroup) {  
  107.         ExpandableListHolder holder = null;  
  108.         if (convertView == null) {  
  109.             convertView = mChildInflater.inflate(R.layout.main_tree_child, null);  
  110.   
  111.             holder = new ExpandableListHolder();  
  112.             holder.nickName = (TextView) convertView.findViewById(R.id.mainChildText1);  
  113.             holder.ioc = (ImageView) convertView.findViewById(R.id.mainChildIcoId);  
  114.             holder.phone = (TextView) convertView.findViewById(R.id.mainChildText2);  
  115.             convertView.setTag(holder);  
  116.         } else {//若行已初始化,直接从tag属性获得子视图的引用  
  117.             holder = (ExpandableListHolder) convertView.getTag();  
  118.         }   
  119.         Map<String,Object> unitData=this.childData.get(groupPosition).get(childPosition);  
  120.         holder.nickName.setText((String)unitData.get("nickName"));  
  121.         holder.ioc.setImageResource((Integer) unitData.get("ico"));  
  122.         holder.phone.setText((String)unitData.get("phone"));  
  123.         return convertView;  
  124.     }  
  125. }  
 

 

 

 

接下来要做的就是  利用自定义的适配器。  添加盟数据进行显了。

 

1、建一个 xml  设样式并设id   

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:listSelector="@color/white"  
  6.     android:orientation="vertical" >  
  7.   
  8. <ExpandableListView  
  9.             android:id="@+id/expandable_id"  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="fill_parent"  
  12.             android:background="@color/white"  
  13.             android:drawSelectorOnTop="false"  
  14.             android:listSelector="@color/white" />  
  15.   
  16. </LinearLayout>  
 

 

 

创建activity

Java代码   收藏代码
  1. public class MainActivity extends Activity {  
  2.     // 声明对象  
  3.     private MainListExpandableListAdapter adapter = null;  
  4.     List<Map<String, Object>> groups;  
  5.     List<List<Map<String, Object>>> childs;  
  6.     ExpandableListView expandableListView;  
  7.   
  8.     private FriendsDao friendsDao;  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.           
  14.         friendsDao=new FriendsDao(this,"ll1x.db",null,2);  
  15.           
  16.         //为ExpandableListView准备数据  
  17.         groups = new ArrayList<Map<String, Object>>();  
  18.         Map<String, Object> group = new HashMap<String, Object>();  
  19.         group.put("title""我的家人");  
  20.         groups.add(group);  
  21.   
  22.         List<Map<String, Object>> child1 = new ArrayList<Map<String, Object>>();  
  23.         Cursor cursor = friendsDao.selectAll();  
  24.         while(cursor.moveToNext()){  
  25.             Map<String, Object> child1Data1 = new HashMap<String, Object>();  
  26.             child1Data1.put("nickName", cursor.getString(cursor.getColumnIndex("nickName")));  
  27.             child1Data1.put("phone", cursor.getString(cursor.getColumnIndex("phone")));  
  28.             child1Data1.put("ico", R.drawable.icon);  
  29.             child1.add(child1Data1);  
  30.         }  
  31.   
  32.         childs = new ArrayList<List<Map<String, Object>>>();  
  33.         childs.add(child1);  
  34.   
  35.         // 实例化ExpandableListView对象  
  36.         expandableListView = (ExpandableListView) findViewById(R.id.expandable_id);  
  37.         // 实例化ExpandableListView的适配器  
  38.         adapter = new MainListExpandableListAdapter(getApplicationContext(), groups, childs);  
  39.   
  40.         // 设置适配器  
  41.         expandableListView.setAdapter(adapter);  
  42.   
  43.         // 设置监听器  
  44.         expandableListView.setOnChildClickListener(new OnChildClickListener() {  
  45.   
  46.             public boolean onChildClick(ExpandableListView parent, View v,  
  47.                     int groupPosition, int childPosition, long id) {  
  48.                 Log.d("test""GroupPosition is " + groupPosition);  
  49.                 Log.d("test""ChildPosition is" + childPosition);  
  50.                 return false;  
  51.             }  
  52.         });  
  53.     }  
  54. }  
 

 

ok 可了。可以放到项目当去了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值