ExpandableListView的简单例子

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

1:main.xml的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/linearLayout"  
     android:layout_width="fill_parent"   
     android:layout_height="fill_parent"  
     androidrientation="vertical"  
     >  
       
     <ExpandableListView  
     android:id="@+id/expandableListView"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
         />  
</LinearLayout> 

显示一个ExpandableListView


2:ExpandableListViewDemo 类的内容

package com.chama.expandablelistviewdemo;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ExpandableListViewDemo extends Activity {
	
	 //定义两个List,用来存放控件中Group/Child中的String
	private List<String> groupArray;        
	private List<List<String>> childArray;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //对这两个List进行初始化,并插入一些数据
         groupArray = new ArrayList<String>();  
         childArray = new ArrayList<List<String>>();  
          
         groupArray.add("第一行");  
         groupArray.add("第二行");  
           
         List<String> tempArray = new ArrayList<String>();  
         tempArray.add("第一条");  
         tempArray.add("第二条");  
         tempArray.add("第三条");  
          
        for(int index = 0; index <groupArray.size(); ++index)  
        {  
           childArray.add(tempArray);  
       }  
        
       //给定义好的ExpandableListView添加上Adapter
       ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
       ExpandableAdapter adapter = new ExpandableAdapter(this);
       expandableListView.setAdapter(adapter);
       
    }
    
    //定义ExpandableListView的Adapter
     public class ExpandableAdapter extends BaseExpandableListAdapter  
     {  
         Activity activity;  
           
         public ExpandableAdapter(Activity a)  
         {  
             activity = a;  
         }  
         
         public Object getChild(int groupPosition, int childPosition)  
         {  
             return childArray.get(groupPosition).get(childPosition);  
         } 
         
         public long getChildId(int groupPosition, int childPosition)  
         {  
             return childPosition;  
         } 
         
         public int getChildrenCount(int groupPosition)  
         {  
             return childArray.get(groupPosition).size();  
         } 
         
         public View getChildView(int groupPosition, int childPosition,  
                 boolean isLastChild, View convertView, ViewGroup parent)  
         {  
             String string = childArray.get(groupPosition).get(childPosition);  
             return getGenericView(string);  
         }  
         
         // group method stub  
         public Object getGroup(int groupPosition)  
         {  
             return groupArray.get(groupPosition);  
         }  
         
         public int getGroupCount()  
         {  
             return groupArray.size();  
         } 
         
         public long getGroupId(int groupPosition)  
         {  
             return groupPosition;  
         }  
         
         public View getGroupView(int groupPosition, boolean isExpanded,  
                 View convertView, ViewGroup parent)  
         {  
             String string = groupArray.get(groupPosition);  
             return getGenericView(string);  
         }  
         
         // View stub to create Group/Children 's View  
         public TextView getGenericView(String string)  
         {  
             // Layout parameters for the ExpandableListView  
             AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(  
                     ViewGroup.LayoutParams.FILL_PARENT, 64);  
             TextView text = new TextView(activity);  
             text.setLayoutParams(layoutParams);  
             // Center the text vertically  
             text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
             // Set the text starting position  
             text.setPadding(36, 0, 0, 0);  
             text.setText(string);  
             return text;  
         }  
         
        public boolean hasStableIds()  
         {  
             return false;  
         }  
         public boolean isChildSelectable(int groupPosition, int childPosition)  
         {  
             return true;  
         }  
     }  
}

需要注意的内容:

  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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波哥的技术积累

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值