本例说明:
QQ,飞信等聊天工具中组后面后会显示有多少个子条目,这个是如何实现的呢?查阅了网上还没有相关的介绍,研究之后,现在本文介绍两种方式实现此功能.
第一种方式:自定义Adapter,重写getGroupView方法.
第二种方式:自定义group.xml中的控件,加一个textview用于显示子条目个数.
注:本文数据库处理使用框架AHibernate,可以灵活操作sqlite数据库,详见: http://blog.csdn.net/lk_blog/article/details/7455992
本文只介绍主要部分,更多细节看上一篇博客:http://blog.csdn.net/lk_blog/article/details/7562987
效果图:
第一种方式主要代码:
MyExpListAdapter.java
package com.tgb.lk.demo.util;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class MyExpListAdapter extends SimpleExpandableListAdapter {
private List<? extends Map<String, ?>> mGroupData;
private String[] mGroupFrom;
private int[] mGroupTo;
MyExpListAdapter(Context context, List<? extends Map<String, ?>> groupData,
int groupLayout, String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, groupLayout, groupLayout, groupFrom, groupTo,
childData, childLayout, childLayout, childFrom, childTo);
mGroupData = groupData;
mGroupFrom = groupFrom;
mGroupTo = groupTo;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newGroupView(isExpanded, parent);
} else {
v = convertView;
}
bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo,
groupPosition);
return v;
}
private void bindView(View view, Map<String, ?> data, String[] from,
int[] to, int groupPosition) {
int len = to.length;
for (int i = 0; i < len; i++) {
TextView v = (TextView) view.findViewById(to[i]);
if (v != null) {
if (i == 1) {
// 这里实现组内有多少子条目数
v.setText((String) data.get(from[i]) + " ("
+ getChildrenCount(groupPosition) + ")");
} else {
v.setText((String) data.get(from[i]));
}
}
}
}
}
GroupChildUtil.java修改:
SimpleExpandableListAdapter sela = new MyExpListAdapter(context,
groupData, groupLayout, new String[] { groupIdFrom,
groupNameFrom }, new int[] { groupIdTo, groupNameTo },
childData, childLayout, new String[] { childIdFrom,
childNameFrom }, new int[] { childIdTo, childNameTo });
return sela;
第二种方式:
list_group.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/groupIdTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/groupNameTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10px"
android:paddingLeft="30px"
android:paddingTop="10px"
android:text="No data"
android:textSize="26sp" />
<TextView
android:id="@+id/countTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10px"
android:paddingLeft="1px"
android:paddingTop="10px"
android:textColor="#00ff00"
android:textSize="26sp" />
</LinearLayout>
GroupChildUtil.java:
package com.tgb.lk.demo.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.tgb.lk.expandable.R;
import android.content.Context;
import android.text.TextUtils;
import android.widget.SimpleExpandableListAdapter;
public class GroupChildUtil {
// 组布局文件id
private static int groupLayout = R.layout.list_group;
// 内容布局文件id
private static int childLayout = R.layout.list_child;
// 绑定的组Id的名字
public static String groupIdFrom = "group_id";
// 绑定的组name的名字
public static String groupNameFrom = "group_name";
// 绑定的内容Id的名字
public static String childIdFrom = "child_id";
// 绑定的内容Name的名字
public static String childNameFrom = "child_name";
// 组Id绑定控件Id
private static int groupIdTo = R.id.groupIdTo;
// 内容Id绑定控件的Id
private static int childIdTo = R.id.childIdTo;
// 组名称绑定的控件Id
private static int groupNameTo = R.id.groupNameTo;
// 内容Id绑定的控件Id
private static int childNameTo = R.id.childNameTo;
// 组名称后绑定显示数量的名字
private static String countFrom = "count";
// 组名称后绑定显示数量的Id
private static int countTo = R.id.countTo;
public static SimpleExpandableListAdapter buildAdapter(Context context,
List<GroupChild> groupChildData) {
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
Map<Map<String, String>, List<Map<String, String>>> tempMap = new LinkedHashMap<Map<String, String>, List<Map<String, String>>>();
if (groupChildData != null && groupChildData.size() > 0) {
for (int i = 0; i < groupChildData.size(); i++) {
GroupChild gc = groupChildData.get(i);
Map<String, String> groupMap = new HashMap<String, String>();
Map<String, String> childMap = new HashMap<String, String>();
groupMap.put(groupIdFrom, gc.getGroupId());
groupMap.put(groupNameFrom, gc.getGroupName());
if (TextUtils.isEmpty(gc.getChildId())
&& TextUtils.isEmpty(gc.getChildName())) {
childMap = null;
} else {
childMap.put(childIdFrom, gc.getChildId());
childMap.put(childNameFrom, gc.getChildName());
}
if (tempMap.containsKey(groupMap)) {
if (childMap != null) {
tempMap.get(groupMap).add(childMap);
}
} else {
List<Map<String, String>> tempList = new ArrayList<Map<String, String>>();
if (childMap != null) {
tempList.add(childMap);
}
tempMap.put(groupMap, tempList);
}
}
}
for (Map<String, String> key : tempMap.keySet()) {
List<Map<String, String>> childList = tempMap.get(key);
key.put(countFrom, "("+String.valueOf(childList.size())+")");
groupData.add(key);
childData.add(childList);
}
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
context, groupData, groupLayout, new String[] { groupIdFrom,
groupNameFrom, countFrom }, new int[] { groupIdTo,
groupNameTo, countTo }, childData, childLayout,
new String[] { childIdFrom, childNameFrom }, new int[] {
childIdTo, childNameTo });
return sela;
}
}
ExpandableListView实例(一)_数据库增删改查处理和listitem点击长按处理
http://blog.csdn.net/lk_blog/article/details/7562987
ExpandableListView实例(二)_两种方式实现QQ中组后面显示子条目数量效果
http://blog.csdn.net/lk_blog/article/details/7563355
ExpandableListView实例(三)_实现QQ中"未分组"效果和"未分组"不可编辑删除功能
http://blog.csdn.net/lk_blog/article/details/7563371
源代码下载地址:http://download.csdn.net/detail/lk_blog/4299729
分享到:
相关推荐
ExpandableListView实例(二)_两种方式实现QQ中组后面显示子条目数量效果 本例说明: QQ,飞信等聊天工具中组后面后会显示有多少个子条目,这个是如何实现的呢?查阅了网上还没有相关的介绍,现在本文介绍两种方式实现此...
ExpandableListView实例(二)_两种方式实现QQ中组后面显示子条目数量效果 本例说明: QQ,飞信等聊天工具中组后面后会显示有多少个子条目,这个是如何实现的呢?查阅了网上还没有相关的介绍,现在本文介绍两种方式实现此...
前几篇文章介绍了Listview,但在实际开发中也经常会用到多层的Listview来展示数据,比如qq中的好友展示,所以这张来了解一下ExpandableListview,基本思想与Listview大致是相同的,所以用起来会比较方便。
模仿QQ好友列表的ExpandableListView实现的效果 现在已实现一个封装好的类出来,只需要把此控件放到RelativeLayout容器中,然后在其Adapter的自定义对象中实现一个HideGroup方法。就可以在Activity里不使用代码实现...
自定义expandablelistview一级标题,并实现类似radiobutton单选效果
ExpandableListView实现购物车页面
ExpandableListView实现时间轴效果 ,不会让大家失望的!!!效果不错,博客有截图
使用expandablelistview实现qq好友分组的功能,代码挺简单的,有注释
这个demo主要介绍了扩展ListView的使用,主要是一个仿QQ列表的小功能,希望可以帮助需要的同学.
本实例简单介绍了android ExpandableListView的使用方法,可以在本例代码基础上稍作修改直接用上项目,或者经简单扩展实现更为复杂的多级列表显示。
ExpandableListView中不同条目的位置不同的显示位置
因为ListView只能显示一级列表,如果实现像QQ好友列表那样的效果,就需要用到ExpandableListView,入门新手可能对该控件不是很熟悉,通过本Android源码你将了解一下基本用法,其实跟ListView差不多。 注:测试时...
android--ExpandableListView实例
在做像QQ好友列表展开时展开项一直漂浮在页面最上面,当下一个父列表滑动到最上面时漂浮栏随从滑出屏幕,我知道可能用expandablelistview但漂浮的效果一直做不好,纠结了好久弄得差不多了,希望对大家有用共同学习也...
ExpandableListView demo练习,实现新增组,删除组,重命名组的功能
Android实现自定义适配器的ExpandableListView示例,准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2",准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2",...
基于Android中的ExpandableListView开发示例, 应用list集合类型
自定义ExpandableListView结合Sqlite,实现listview点击收缩效果
ExpandableListView (实现ListView嵌套ListView的效果)