具体评论ExpandableListView显示和查询模仿QQ组列表用户信息

时间:2022-09-12 18:14:07

在我们的项目开发过程,用户通常拥有的信息包,通过组来显示用户的信息,一时候通过一定的查询条件来显示查询后的相关用户信息。而且通过颜色选择器来设置列表信息的背景颜色。

当中借鉴xiaanming:http://blog.csdn.net/xiaanming/article/details/12684155

以下来看看项目执行后的效果图以及代码结构图:

具体评论ExpandableListView显示和查询模仿QQ组列表用户信息具体评论ExpandableListView显示和查询模仿QQ组列表用户信息具体评论ExpandableListView显示和查询模仿QQ组列表用户信息具体评论ExpandableListView显示和查询模仿QQ组列表用户信息

以下通过代码来实现整个效果。

1.主界面布局activity_main.xml

<span style="font-size:18px;"><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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" > <Button
android:id="@+id/my_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生成部门列表" /> <com.example.myexpandabledemo.ClearEditText
android:id="@+id/filter_edit"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="10dip"
android:background="@drawable/search_bar_edit_selector"
android:drawableLeft="@drawable/search_bar_icon_normal"
android:hint="搜索"
android:singleLine="true"
android:textSize="15.0dip" />
<TextView
android:id="@+id/no_search_result_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="未搜索到结果"
android:visibility="gone"
/>
<ExpandableListView
android:id="@+id/my_expand_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout></span>

分析:主界面主要有一个Button,一个自己定义搜索的ClearEditText,一个无查询结果的TextView,一个列表信息显示ExpandableListView。

当中Button主要用来生成列表信息。

2.自己定义的ClearEditText的搜索框CliearEditText.java

<span style="font-size:18px;">package com.example.myexpandabledemo;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText; public class ClearEditText extends EditText implements
OnFocusChangeListener, TextWatcher {
/**
* 删除button的引用
*/
private Drawable mClearDrawable; public ClearEditText(Context context) {
this(context, null);
} public ClearEditText(Context context, AttributeSet attrs) {
//这里构造方法也非常重要,不加这个非常多属性不能再XML里面定义
this(context, attrs, android.R.attr.editTextStyle);
} public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
//获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
mClearDrawable = getResources()
.getDrawable(R.drawable.emotionstore_progresscancelbtn);
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
setClearIconVisible(false);
setOnFocusChangeListener(this);
addTextChangedListener(this);
} /**
* 由于我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
* 当我们按下的位置 在 EditText的宽度 - 图标到控件右边的间距 - 图标的宽度 和
* EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (getCompoundDrawables()[2] != null) {
if (event.getAction() == MotionEvent.ACTION_UP) {
boolean touchable = event.getX() > (getWidth()
- getPaddingRight() - mClearDrawable.getIntrinsicWidth())
&& (event.getX() < ((getWidth() - getPaddingRight())));
if (touchable) {
this.setText("");
}
}
} return super.onTouchEvent(event);
} /**
* 当ClearEditText焦点发生变化的时候,推断里面字符串长度设置清除图标的显示与隐藏
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
} /**
* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
* @param visible
*/
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
} /**
* 当输入框里面内容发生变化的时候回调的方法
*/
@Override
public void onTextChanged(CharSequence s, int start, int count,
int after) {
setClearIconVisible(s.length() > 0);
} @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) { } @Override
public void afterTextChanged(Editable s) { } /**
* 设置晃动动画
*/
public void setShakeAnimation(){
this.setAnimation(shakeAnimation(5));
} /**
* 晃动动画
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
} }</span>

搜索框颜色选择器:

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/search_bar_edit_pressed"></item>
<item android:state_pressed="true" android:drawable="@drawable/search_bar_edit_pressed"></item>
<item android:drawable="@drawable/search_bar_edit_normal"></item>
</selector></span>

3.汉字拼音排序所须要的汉字转拼音:CharacterParser.java

<span style="font-size:18px;">package com.example.myexpandabledemo;

/**
* Java汉字转换为拼音
*
*/
public class CharacterParser {
private static int[] pyvalue = new int[] {-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032,
-20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,
-19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261,
-19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961,
-18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490,
-18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988,
-17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692,
-17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733,
-16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419,
-16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,
-15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631,
-15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180,
-15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941,
-14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857,
-14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353,
-14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094,
-14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831,
-13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,
-13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860,
-12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300,
-12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358,
-11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014,
-10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309,
-10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254};
public static String[] pystr = new String[] {"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian",
"biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che",
"chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan",
"cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du",
"duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang",
"gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang",
"hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian",
"jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken",
"keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng",
"li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai",
"man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai",
"nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan",
"nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu",
"qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re",
"ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha",
"shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun",
"shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao",
"tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi",
"xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi",
"yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha",
"zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui",
"zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"};
private StringBuilder buffer;
private String resource;
private static CharacterParser characterParser = new CharacterParser(); public static CharacterParser getInstance() {
return characterParser;
} public String getResource() {
return resource;
} public void setResource(String resource) {
this.resource = resource;
} /** * 汉字转成ASCII码 * * @param chs * @return */
private int getChsAscii(String chs) {
int asc = 0;
try {
byte[] bytes = chs.getBytes("gb2312");
if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
throw new RuntimeException("illegal resource string");
}
if (bytes.length == 1) {
asc = bytes[0];
}
if (bytes.length == 2) {
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
asc = (256 * hightByte + lowByte) - 256 * 256;
}
} catch (Exception e) {
System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e);
}
return asc;
} /** * 单字解析 * * @param str * @return */
public String convert(String str) {
String result = null;
int ascii = getChsAscii(str);
if (ascii > 0 && ascii < 160) {
result = String.valueOf((char) ascii);
} else {
for (int i = (pyvalue.length - 1); i >= 0; i--) {
if (pyvalue[i] <= ascii) {
result = pystr[i];
break;
}
}
}
return result;
} /** * 词组解析 * * @param chs * @return */
public String getSelling(String chs) {
String key, value;
buffer = new StringBuilder();
for (int i = 0; i < chs.length(); i++) {
key = chs.substring(i, i + 1);
if (key.getBytes().length >= 2) {
value = (String) convert(key);
if (value == null) {
value = "unknown";
}
} else {
value = key;
}
buffer.append(value);
}
return buffer.toString();
} public String getSpelling() {
return this.getSelling(this.getResource());
} }
</span>

4. 拼音比較类:PinyinComparator.java

<span style="font-size:18px;">package com.example.myexpandabledemo;

import java.util.Comparator;

public class PinyinComparator implements Comparator<GroupMemberBean> {

	public int compare(GroupMemberBean o1, GroupMemberBean o2) {
if (o1.getSortLetters().equals("@")
|| o2.getSortLetters().equals("#")) {
return -1;
} else if (o1.getSortLetters().equals("#")
|| o2.getSortLetters().equals("@")) {
return 1;
} else {
return o1.getSortLetters().compareTo(o2.getSortLetters());
}
} }
</span>

5. 显示列表信息的名字以及其拼音首字母 GrouMemberBean.java

<span style="font-size:18px;">package com.example.myexpandabledemo;

public class GroupMemberBean {

	private String name;   //显示的数据
private String sortLetters; //显示数据拼音的首字母 public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSortLetters() {
return sortLetters;
}
public void setSortLetters(String sortLetters) {
this.sortLetters = sortLetters;
}
}
</span>

6.ExpandaleListView 适配器MyExpandableAdapter.java

<span style="font-size:18px;">package com.example.myexpandabledemo;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class MyExpandableAdapter extends BaseExpandableListAdapter { private Context mContext;
private List<GroupMemberBean> mGroup;
private List<List<GroupMemberBean>> mChild;
private LayoutInflater mInflater;
public MyExpandableAdapter(Context mContext,List<GroupMemberBean> mGroup,List<List<GroupMemberBean>> mChild){
this.mContext = mContext;
this.mGroup = mGroup;
this.mChild = mChild;
this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* 当ListView数据发生变化时,调用此方法来更新ListView
*
* @param list
*/
public void updateListView(List<GroupMemberBean> mGroup,List<List<GroupMemberBean>> mChild) {
this.mGroup = mGroup;
this.mChild = mChild;
notifyDataSetChanged();
} @Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mGroup.size();
} @Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mChild.get(groupPosition).size();
} @Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return mGroup.get(groupPosition);
} @Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return mChild.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 false;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupHolderView groupHolderView;
if(convertView == null){
groupHolderView = new GroupHolderView();
convertView = (View) mInflater.inflate(R.layout.activity_group, null);
groupHolderView.groupTv = (TextView) convertView.findViewById(R.id.my_group_tv);
convertView.setTag(groupHolderView);
}else{
groupHolderView = (GroupHolderView) convertView.getTag();
}
groupHolderView.groupTv.setText(mGroup.get(groupPosition).getName());
return convertView;
} @Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub ChildHolderView childHolderView;
if(convertView == null){
childHolderView = new ChildHolderView();
convertView = (View) mInflater.inflate(R.layout.activity_child, null);
childHolderView.childIv = (ImageView) convertView.findViewById(R.id.my_child_iv);
childHolderView.childTv = (TextView) convertView.findViewById(R.id.my_child_tv);
childHolderView.childLl = (LinearLayout) convertView.findViewById(R.id.my_child_ll);
convertView.setTag(childHolderView);
}else{
childHolderView = (ChildHolderView) convertView.getTag();
}
childHolderView.childTv.setText(mChild.get(groupPosition).get(childPosition).getName());
childHolderView.childLl.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "group:" + mGroup.get(groupPosition).getName() + "->child:" + mChild.get(groupPosition).get(childPosition).getName(), Toast.LENGTH_SHORT).show();
}
}); return convertView;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
} class GroupHolderView{
TextView groupTv;
} class ChildHolderView{
ImageView childIv;
TextView childTv;
LinearLayout childLl;
} }</span>

分析:

GroupHolderView:用于显示组信息

ChildHolderView:用于显示组下成员信息。包含一个LinearLayout,一个ImageView,一个TextVIew

private List<GroupMemberBean> mGroup; 包括组的全部List信息

private List<List<GroupMemberBean>> mChild;包括全部的组下成员信息

<span style="font-size:18px;">	public void updateListView(List<GroupMemberBean> mGroup,List<List<GroupMemberBean>> mChild) {
this.mGroup = mGroup;
this.mChild = mChild;
notifyDataSetChanged();
}</span>

依据传入的mGroup,mChild来更新列表信息。

7.GroupHolderView类所相应的activity_group.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/my_group_tv"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:paddingLeft="50dp"
android:gravity="center_vertical"
/> </LinearLayout></span>

8.ChildHolderView类所相应的activity_child.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_child_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_child_selector"
android:orientation="horizontal" > <ImageView
android:id="@+id/my_child_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" /> <TextView
android:id="@+id/my_child_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout></span>

分析:依据android:background="@drawable/my_child_selector" 颜色选择器来设置每一个列表行(选中或正常)的背景色。

9.颜色选择器my_child_selector.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?

>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/press_color"></item>
<item android:state_pressed="true" android:drawable="@color/press_color"></item>
<item android:drawable="@color/normal_color"></item>
</selector></span>

10.主界面实现代码:MainActivity.java

<span style="font-size:18px;">package com.example.myexpandabledemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView; public class MainActivity extends Activity { private Button myBtn;
private ExpandableListView myExpandLv;
private ClearEditText myClearEt;
private TextView noSearchResultTv;
private MyExpandableAdapter myAdapter; private List<GroupMemberBean> groupBeanList;
private List<List<GroupMemberBean>> childBeanList = new ArrayList<List<GroupMemberBean>>();
/**
* 汉字转换成拼音的类
*/
private CharacterParser characterParser;
/**
* 依据拼音来排列ListView里面的数据类
*/
private PinyinComparator pinyinComparator; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init();
} private void init() {
noSearchResultTv = (TextView)this.findViewById(R.id.no_search_result_tv);
myExpandLv = (ExpandableListView) this.findViewById(R.id.my_expand_lv); // 实例化汉字转拼音类
characterParser = CharacterParser.getInstance(); pinyinComparator = new PinyinComparator(); groupBeanList = filledData(this.getResources().getStringArray(
R.array.depart));
List<GroupMemberBean> tempOne = filledData(this.getResources()
.getStringArray(R.array.child_one));
List<GroupMemberBean> tempTwo = filledData(this.getResources()
.getStringArray(R.array.child_two)); // 依据a-z进行排序源数据
Collections.sort(groupBeanList, pinyinComparator); Collections.sort(tempOne, pinyinComparator);
Collections.sort(tempTwo, pinyinComparator); for (int i = 0; i < groupBeanList.size(); i++) {
if (i % 2 == 0) {
childBeanList.add(tempOne);
} else {
childBeanList.add(tempTwo);
}
} //用于生成列表信息
myBtn = (Button) this.findViewById(R.id.my_btn);
myBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
myAdapter = new MyExpandableAdapter(MainActivity.this,
groupBeanList, childBeanList);
myExpandLv.setAdapter(myAdapter);
myExpandLv.expandGroup(0);
} }); myClearEt = (ClearEditText) this.findViewById(R.id.filter_edit); myClearEt.addTextChangedListener(new TextWatcher() { @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub } @Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
filterData(s.toString());
} @Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub }
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* 为ListView填充数据
*
* @param date
* @return
*/
private List<GroupMemberBean> filledData(String[] date) {
List<GroupMemberBean> mSortList = new ArrayList<GroupMemberBean>(); for (int i = 0; i < date.length; i++) {
GroupMemberBean sortModel = new GroupMemberBean();
sortModel.setName(date[i]);
// 汉字转换成拼音
String pinyin = characterParser.getSelling(date[i]);
String sortString = pinyin.substring(0, 1).toUpperCase(); // 正則表達式。推断首字母是否是英文字母
if (sortString.matches("[A-Z]")) {
sortModel.setSortLetters(sortString.toUpperCase());
} else {
sortModel.setSortLetters("#");
} mSortList.add(sortModel);
}
return mSortList; } /**
* 依据输入框中的值来过滤数据并更新ListView
*
* @param filterStr
*/
private void filterData(String filterStr) {
List<GroupMemberBean> groupFilterList = new ArrayList<GroupMemberBean>();
List<GroupMemberBean> tempFilterList;
List<List<GroupMemberBean>> childFilterList = new ArrayList<List<GroupMemberBean>>(); if (TextUtils.isEmpty(filterStr)) {
groupFilterList = groupBeanList;
childFilterList = childBeanList;
noSearchResultTv.setVisibility(View.GONE);
} else {
groupFilterList.clear();
childFilterList.clear();
for (int i = 0; i < groupBeanList.size(); i++) {
//标记departGroup是否增加元素
boolean isAddGroup = false;
tempFilterList = new ArrayList<GroupMemberBean>();
GroupMemberBean sortModel = groupBeanList.get(i);
String name = sortModel.getName();
// depart有字符直接增加
if (name.indexOf(filterStr.toString()) != -1
|| characterParser.getSelling(name).startsWith(
filterStr.toString())) {
if (!groupFilterList.contains(sortModel)) {
groupFilterList.add(sortModel);
isAddGroup = true;
}
} for (int j = 0; j < childBeanList.get(i).size(); j++) {
GroupMemberBean sortChildModel = childBeanList.get(i)
.get(j);
String childName = sortChildModel.getName();
// child有字符直接增加,其父也增加
if (childName.indexOf(filterStr.toString()) != -1
|| characterParser.getSelling(childName)
.startsWith(filterStr.toString())) {
tempFilterList.add(sortChildModel);
if (!groupFilterList.contains(groupBeanList.get(i))) {
groupFilterList.add(groupBeanList.get(i));
isAddGroup = true;
}
} }
Collections.sort(tempFilterList, pinyinComparator);
if (isAddGroup) {
childFilterList.add(tempFilterList);
}
} // 依据a-z进行排序
Collections.sort(groupBeanList, pinyinComparator);
} if (myAdapter != null) {
myAdapter.updateListView(groupFilterList, childFilterList); if (TextUtils.isEmpty(filterStr)) {
for (int i = 0; i < groupFilterList.size(); i++) {
if (i == 0) {
myExpandLv.expandGroup(i);
continue;
}
myExpandLv.collapseGroup(i);
}
} else {
//搜索的结果所有展开
for (int i = 0; i < groupFilterList.size(); i++) {
myExpandLv.expandGroup(i);
}
}
} //假设查询的结果为0时,显示为搜索到结果的提示
if(groupFilterList.size() == 0){
noSearchResultTv.setVisibility(View.VISIBLE);
}else{
noSearchResultTv.setVisibility(View.GONE);
}
}
}
</span>

分析:

<span style="font-size:18px;">		myBtn.setOnClickListener(new OnClickListener() {

			@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myAdapter = new MyExpandableAdapter(MainActivity.this,
groupBeanList, childBeanList);
myExpandLv.setAdapter(myAdapter);
myExpandLv.expandGroup(0);
} });</span>

用于生成列表信息

<span style="font-size:18px;">private List<GroupMemberBean> filledData(String[] date) </span>

依据 res->array定义的字符数组,转化为List<GroupMemberBean>

<span style="font-size:18px;">private void filterData(String filterStr) {</span>

依据查询条件更新列表信息。

重点:1.假设查询的条件为空,直接更新列表初始状态;

假如:filterStr = “g”;

2.假设查询的结果为空,直接给出“未搜索到结果”提示;

3.假设Group中包括filterStr,GroupMemberBean sortModel 直接增加groupFilterList,并标记已增加isAddGroup = true;

假设Child中包括filterStr,定义暂时的tempFilterList,GroupMemberBean sortChildModel增加到childFilterList。假设groupFilterList不包括其父组,并将其父增加到groupFilterList。并标记已增加isAddGroup=true;

最后假设isAddGroup=true,将tempFilterList增加到childFilterList。

4.通过myAdapter.updateListView(groupFilterList, childFilterList),更新列表信息。

其上为本博文的全部内容。

源码下载:

http://download.csdn.net/detail/a123demi/7623975

版权声明:本文博客原创文章,博客,未经同意,不得转载。

具体评论ExpandableListView显示和查询模仿QQ组列表用户信息的更多相关文章

  1. Linux用户信息查询

    1 查询目前已登录的用户信息w 或者 who [@bjzw_11_210 ~]# w :: up days, :, users, load average: 0.03, 0.04, 0.00 USER ...

  2. 显示当前用户所拥有的表&amp&semi;当前用户可以访问的所有表&amp&semi;数据库中的所有表&amp&semi;当前用户信息&amp&semi;当前用户所能管理的用户&amp&semi;数据库中所拥有的用户

    1)显示当前用户名称:show user或者show user; 2)显示当前用户信息(包括用户名之外还有其他信息):select * from user_users; 3)显示数据库中所含有的所有用 ...

  3. 模仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(上)

      我们大部分人都发过动态,想必都知道发动态.回复评论.删除动态的整个过程,那么作为初学者,要模仿这些功能有点复杂的,最起码表的关系得弄清楚~~ 先把思路理一下: (1)用户登录,用session读取 ...

  4. 活跃天数计算用户等级模仿QQ的升级方式

    QQ等级的算法:设当前等级为N,达到当前等级最少需要的活跃天数为D,当前活跃天数为Dc,升级剩余天数为Dr,则: 从而推出: 好了,引述完成,懒得写字了,贴出代码: 复制内容到剪贴板 代码: < ...

  5. 模仿QQ空间 网页设计

    目的:1.通过模仿QQ空间,全自主写代码,熟悉网页设计的流程 2.熟练的掌握HTML.CSS.JS的应用 3.将在此过程中遇到的问题及其解决方法记录在此,以便取用. 开始: 一.登陆界面(index. ...

  6. Android ExpandableListView BaseExpandableListAdapter &lpar;类似QQ分组列表)

    分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...

  7. 模仿QQ气泡聊天

    尝试了几种方案,想模仿QQ的气泡聊天,总是不尽如意.网上倒是大把的Android和Html的例子,Delphi的没找着,只能自己试着折腾. 1. 用WebBrowser加载本地html,屡次折腾,失败 ...

  8. Qt 之 模仿 QQ登陆界面——样式篇

    一.简述 今天晚上花了半天时间从QQ登录界面抠了些图,顺便加了点样式基本上实现了QQ的登陆界面全部效果.虽不说100%相似,那也有99.99%相似了哈O(∩_∩)O. QQ好像从去年开始,登录界面有了 ...

  9. iOS之基于FreeStreamer的简单音乐播放器&lpar;模仿QQ音乐&rpar;

    代码地址如下:http://www.demodashi.com/demo/11944.html 天道酬勤 前言 作为一名iOS开发者,每当使用APP的时候,总难免会情不自禁的去想想,这个怎么做的?该怎 ...

随机推荐

  1. traits的使用

    trait的作用是可以在任何地方使用trait中的方法. trait的定义与定义类相同,定义实例如下: trait tSoneTrait{ //定义一些属性 function someFunction ...

  2. linux i2c 设备节点读写

    最近需要操作24C02,封装了一下函数方便以后操作. 参考链接: https://my.oschina.net/handawei/blog/68526 http://blog.csdn.net/one ...

  3. Bootstrap 列偏移&bsol;列嵌套&bsol;列排序

    1.列偏移 这个其实很简单就是通过一个样式类,通过.col-md-offset-*可以将列偏移到右侧.这些class通过使用*选择器将所有列增加了列的左侧margin.例如,.col-md-offse ...

  4. dreamvc框架&lpar;一&rpar;ioc容器的集成

    我的dreamvc框架最终写得差点儿相同了,借鉴了非常多开源框架,SpringMVC.Struts2等,眼下放在github上面.地址请猛戳我 写得差点儿相同了,是要写一个总结,把自己当时的思路记录下 ...

  5. iOS7&period;0中UILabel高度调整注意事项(转)

    注释:原文链接丢失. 我的“记词助手”在升级到iOS7之后,一直出现UILabel错位的问题: 我的label是用- (CGSize)sizeWithFont:(UIFont *)font const ...

  6. Designing Data-Intensive Applications

    下面是这本书序言中的大部分内容,本人的英文水平有限,有理解不到位的地方还请大家指教,这算是自己对这本书的读书笔记和总结. 数据是当今系统设计中许多挑战的中心,一些难以解决的问题如系统的可扩展性,一致性 ...

  7. Python基础(time模块,datetime模块)

    #Author : Kelvin #Date : 2019/1/6 15:10 import time #获取此时的时间戳(从此刻到1970年一月一号零点的秒数) res1=time.time() p ...

  8. OpenCV 的颜色空间转换

    # coding: utf-8 ''' 第13章主要介绍:颜色空间转换 ''' import cv2 import numpy as np ''' 经常用到的颜色空间转换是: BGR<-> ...

  9. 【系列教程1】Gradle入门系列一:简介

    Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注. 这篇文章是Gradle教程的第一篇,我们有两个目 ...

  10. &period;net写本地文件的一个方法

    整理代码,.net在本地写html文件的一个方法,代码如下 public static void WriteFile(string FilePath, string FileInfo, string ...