ExpandableListView的简单例子

最近一段时间参考网上的例子,做了一下简单的 ExpandableListView,现在和大家共享一下:

1:main.xml的内容

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout     
  3.      xmlns:android="http://schemas.android.com/apk/res/android"  
  4.      android:id="@+id/linearLayout"    
  5.      android:layout_width="fill_parent"     
  6.      android:layout_height="fill_parent"    
  7.      androidrientation="vertical"    
  8.      >    
  9.          
  10.      <ExpandableListView    
  11.      android:id="@+id/expandableListView"    
  12.      android:layout_width="fill_parent"    
  13.      android:layout_height="wrap_content"    
  14.          />    
  15. </LinearLayout>   

显示一个ExpandableListView


2:ExpandableListViewDemo 类的内容

[java]  view plain copy
  1. package com.chama.expandablelistviewdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.Gravity;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.AbsListView;  
  11. import android.widget.BaseExpandableListAdapter;  
  12. import android.widget.ExpandableListView;  
  13. import android.widget.TextView;  
  14.   
  15. public class ExpandableListViewDemo extends Activity {  
  16.       
  17.      //定义两个List,用来存放控件中Group/Child中的String  
  18.     private List<String> groupArray;          
  19.     private List<List<String>> childArray;  
  20.       
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.           
  27.         //对这两个List进行初始化,并插入一些数据  
  28.          groupArray = new ArrayList<String>();    
  29.          childArray = new ArrayList<List<String>>();    
  30.             
  31.          groupArray.add("第一行");    
  32.          groupArray.add("第二行");    
  33.              
  34.          List<String> tempArray = new ArrayList<String>();    
  35.          tempArray.add("第一条");    
  36.          tempArray.add("第二条");    
  37.          tempArray.add("第三条");    
  38.             
  39.         for(int index = 0; index <groupArray.size(); ++index)    
  40.         {    
  41.            childArray.add(tempArray);    
  42.        }    
  43.           
  44.        //给定义好的ExpandableListView添加上Adapter  
  45.        ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);  
  46.        ExpandableAdapter adapter = new ExpandableAdapter(this);  
  47.        expandableListView.setAdapter(adapter);  
  48.          
  49.     }  
  50.       
  51.     //定义ExpandableListView的Adapter  
  52.      public class ExpandableAdapter extends BaseExpandableListAdapter    
  53.      {    
  54.          Activity activity;    
  55.              
  56.          public ExpandableAdapter(Activity a)    
  57.          {    
  58.              activity = a;    
  59.          }    
  60.            
  61.          public Object getChild(int groupPosition, int childPosition)    
  62.          {    
  63.              return childArray.get(groupPosition).get(childPosition);    
  64.          }   
  65.            
  66.          public long getChildId(int groupPosition, int childPosition)    
  67.          {    
  68.              return childPosition;    
  69.          }   
  70.            
  71.          public int getChildrenCount(int groupPosition)    
  72.          {    
  73.              return childArray.get(groupPosition).size();    
  74.          }   
  75.            
  76.          public View getChildView(int groupPosition, int childPosition,    
  77.                  boolean isLastChild, View convertView, ViewGroup parent)    
  78.          {    
  79.              String string = childArray.get(groupPosition).get(childPosition);    
  80.              return getGenericView(string);    
  81.          }    
  82.            
  83.          // group method stub    
  84.          public Object getGroup(int groupPosition)    
  85.          {    
  86.              return groupArray.get(groupPosition);    
  87.          }    
  88.            
  89.          public int getGroupCount()    
  90.          {    
  91.              return groupArray.size();    
  92.          }   
  93.            
  94.          public long getGroupId(int groupPosition)    
  95.          {    
  96.              return groupPosition;    
  97.          }    
  98.            
  99.          public View getGroupView(int groupPosition, boolean isExpanded,    
  100.                  View convertView, ViewGroup parent)    
  101.          {    
  102.              String string = groupArray.get(groupPosition);    
  103.              return getGenericView(string);    
  104.          }    
  105.            
  106.          // View stub to create Group/Children 's View    
  107.          public TextView getGenericView(String string)    
  108.          {    
  109.              // Layout parameters for the ExpandableListView    
  110.              AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(    
  111.                      ViewGroup.LayoutParams.FILL_PARENT, 64);    
  112.              TextView text = new TextView(activity);    
  113.              text.setLayoutParams(layoutParams);    
  114.              // Center the text vertically    
  115.              text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);    
  116.              // Set the text starting position    
  117.              text.setPadding(36000);    
  118.              text.setText(string);    
  119.              return text;    
  120.          }    
  121.            
  122.         public boolean hasStableIds()    
  123.          {    
  124.              return false;    
  125.          }    
  126.          public boolean isChildSelectable(int groupPosition, int childPosition)    
  127.          {    
  128.              return true;    
  129.          }    
  130.      }    
  131. }  

需要注意的内容:

  1:groupArray 和childArray的内容,分别定义父对象和子对象显示的内容

  2:ExpandableAdapter继承了BaseExpandableListAdapter,并重载了其中的一些方法, 注意public TextView getGenericView(String string)的作用,主要用形成显示父对象和子对象视图的类


3:程序效果

 

ExpandableListView实例(一)_数据库增删改查处理和listitem点击长按处理 本例说明: 1.实例中表现层与数据处理层分开,代码可复用性强,如果能看懂代码对算法会有提高. 2.组和子条目上"点击"事件处理,能够区分操作的是组还是子条目,并且得到组和子条目的内容. 3.组和子条目上"长按"事件处理,能够区分组和子条目,并且得到组和子条目的内容. 4.自定义条目样式,灵活与数据库中字段绑定. 5.实现对DB的增删改查,并且操作后自动刷新. 6.使用数据库处理框架AHibernate灵活操作sqlite数据库,详见: http://blog.csdn.net/lk_blog/article/details/7455992 ExpandableListView实例(二)_两种方式实现QQ中组后面显示子条目数量效果 本例说明: QQ,飞信等聊天工具中组后面后会显示有多少个子条目,这个是如何实现的呢?查阅了网上还没有相关的介绍,现在本文介绍两种方式实现此功能. 第一种方式:自定义Adapter,重写getGroupView方法. 第二种方式:自定义group.xml中的控件,加一个textview用于显示子条目个数. 注:本文数据库处理使用框架AHibernate,可以灵活操作sqlite数据库, 详见: http://blog.csdn.net/lk_blog/article/details/7455992 ExpandableListView实例(三)_实现QQ中"未分组"效果和"未分组"不可编辑删除功能 本例说明: 实现QQ中"未分组"效果和"未分组"不可编辑删除功能. 注:本文数据库处理使用框架AHibernate,可以灵活操作sqlite数据库, 详见: http://blog.csdn.net/lk_blog/article/details/7455992
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值