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>
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; } }