Android ExpandableListView Demo

      喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,但界面优化方面做得还不够好,有待改进,下面直接上效果图以及源代码~!

源码地址:http://download.csdn.net/detail/qqq01002013019/8235673



 



package com.cn.wang;


import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.cn.wang.entity.Person;


public class MainActivity extends Activity {

private ExpandableListView expandableListView;
private List<Person> allperson;  //数据集合
private List<Person> group_list1, group_list2, group_list3, group_list4,
group_list5;
private List<List<Person>> item_list; // 多少组
private String[] flag = { "我的设备", "我的好友", "朋友", "家人", "同学" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

allperson = new ArrayList<Person>();
// 随便一堆测试数据
group_list1 = new ArrayList<Person>();
group_list2 = new ArrayList<Person>();
group_list3 = new ArrayList<Person>();
group_list4 = new ArrayList<Person>();
group_list5 = new ArrayList<Person>();
item_list = new ArrayList<List<Person>>();
allperson.add(new Person("我的设备", R.drawable.ic_launcher, "张三"));
allperson.add(new Person("我的设备", R.drawable.aerobics2, "gfe"));
allperson.add(new Person("我的设备", R.drawable.all_b, "wqre"));
allperson
.add(new Person("我的设备", R.drawable.americanfootball2_b, "rwq"));
allperson.add(new Person("我的好友", R.drawable.ic_launcher, "发送"));
allperson.add(new Person("我的好友", R.drawable.all_b, "wer"));
allperson.add(new Person("我的好友", R.drawable.ic_launcher, "rqwer"));
allperson.add(new Person("朋友", R.drawable.arrow, "文件"));
allperson.add(new Person("朋友", R.drawable.ic_launcher, "tret件"));
allperson.add(new Person("朋友", R.drawable.ic_launcher, "ewrt件"));
allperson
.add(new Person("朋友", R.drawable.americanfootball2_b, "2634件"));
allperson.add(new Person("朋友", R.drawable.ic_launcher, "terw"));
allperson.add(new Person("朋友", R.drawable.all_b, "tewrt"));
allperson.add(new Person("朋友", R.drawable.ic_launcher, "werte"));
allperson.add(new Person("家人", R.drawable.all_b, "我"));
allperson.add(new Person("同学", R.drawable.ic_launcher, "飞洒"));
allperson.add(new Person("同学", R.drawable.aerobics2_b, "rwet"));
allperson.add(new Person("同学", R.drawable.ic_launcher, "rw"));
for (int i = 0; i < allperson.size(); i++) {
if (allperson.get(i).getFlag().equals("我的设备")) {
group_list1.add(allperson.get(i));
}
if (allperson.get(i).getFlag().equals("我的好友")) {
group_list2.add(allperson.get(i));
}
if (allperson.get(i).getFlag().equals("朋友")) {
group_list3.add(allperson.get(i));
}
if (allperson.get(i).getFlag().equals("家人")) {
group_list4.add(allperson.get(i));
}
if (allperson.get(i).getFlag().equals("同学")) {
group_list5.add(allperson.get(i));
}
}
item_list.add(group_list1);
item_list.add(group_list2);
item_list.add(group_list3);
item_list.add(group_list4);
item_list.add(group_list5);
expandableListView = (ExpandableListView) findViewById(R.id.expandlist);
expandableListView.setGroupIndicator(null);
expandableListView.setAdapter(new MyExpandableListViewAdapter(this));


        //设置item点击的监听器
        expandableListView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this,
                        "你点击了" + item_list.get(groupPosition).get(childPosition).getName(),
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
}

private class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;

public MyExpandableListViewAdapter(Context context) {
this.context = context;
}


@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return item_list.size();
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return item_list.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return item_list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return item_list.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupHolder groupHolder = null;
if (convertView == null) {
convertView = (View) getLayoutInflater().from(context).inflate(
R.layout.head, null);
groupHolder = new GroupHolder();
groupHolder.txt = (TextView) convertView.findViewById(R.id.txt);
groupHolder.num = (TextView) convertView.findViewById(R.id.num);
groupHolder.img = (ImageView) convertView
.findViewById(R.id.img);
convertView.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) convertView.getTag();
}
groupHolder.txt.setText(flag[groupPosition]);
groupHolder.num.setText(item_list.get(groupPosition).size()+"/"+allperson.size());
if (isExpanded) {
groupHolder.img.setBackgroundResource(R.drawable.left);
} else {
groupHolder.img.setBackgroundResource(R.drawable.down);
}
return convertView;
}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ItemHolder itemHolder = null;
if (convertView == null) {
convertView = (View) getLayoutInflater().from(context).inflate(
R.layout.item, null);
itemHolder = new ItemHolder();
itemHolder.txt = (TextView) convertView.findViewById(R.id.txt);
itemHolder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(itemHolder);
} else {
itemHolder = (ItemHolder) convertView.getTag();
}
itemHolder.txt.setText(item_list.get(groupPosition)
.get(childPosition).getName());
itemHolder.img.setBackgroundResource(item_list.get(groupPosition)
.get(childPosition).getId());
return convertView;
}

/**
* 设置子项可选
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}


class GroupHolder {
public TextView txt;
public TextView num;
public ImageView img;
}

class ItemHolder {
public ImageView img;
public TextView txt;
}

}


/**

*实体类

/

package com.cn.wang.entity;

public class Person {
private String flag;
private int id;
private String name;

public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String flag, int id, String name) {
this.flag = flag;
this.id = id;
this.name = name;
}
}


//布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F0FFFF"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="30dp" 
        android:background="#cfcfcf"
        android:gravity="center_vertical"
        android:padding="5dp"
        android:text="好友分组" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:background="#F0FFFF"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ExpandableListView
                android:id="@+id/expandlist"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#fff"
                android:clickable="true"
                android:listSelector="#F0FFFF" >
            </ExpandableListView>
        </LinearLayout>


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#DBDBDB" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#F0FFFF" />
    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
       
       
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#f0f"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="5dp"
        android:text="通 知" /> 
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#ff0"
        android:gravity="center"
        android:padding="5dp"
           android:layout_weight="1"
        android:text="联系人" /> 
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#0ff"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="5dp"
        android:text="动 态" /> 
   
        </LinearLayout>
</LinearLayout>


//布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#F0FFFF"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >


    <ImageView
        android:id="@+id/img"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/aerobics2_b" />


    <TextView
        android:id="@+id/txt"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp" />
    
    <TextView
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_marginLeft="5dp" />
</LinearLayout>


//布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
     >
    
    <ImageView 
        android:layout_marginLeft="12dp"
        android:id="@+id/img"
        android:layout_width="20dp"
        android:layout_height="20dp"
        />
    <TextView 
        android:layout_marginLeft="5dp"
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值