hongniu163.xml

3044人阅读
Android(42)
在我们的项目开发过程中,经常会对用户的信息进行分组,即通过组来显示用户的信息,同时通过一定的查询条件来显示查询后的相关用户信息,并且通过颜色选择器来设置列表信息的背景颜色。
其中借鉴xiaanming:http://blog.csdn.net/xiaanming/article/details/
下面来看看项目运行后的效果图以及代码结构图:
下面通过代码来实现整个效果。
1.主界面布局activity_main.xml
&span style=&font-size:18&&&LinearLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/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& &
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& /&
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:18&&package com.example.
import android.content.C
import android.graphics.drawable.D
import android.text.E
import android.text.TextW
import android.util.AttributeS
import android.view.MotionE
import android.view.V
import android.view.View.OnFocusChangeL
import android.view.animation.A
import android.view.animation.CycleI
import android.view.animation.TranslateA
import android.widget.EditT
public class ClearEditText extends EditText implements
OnFocusChangeListener, TextWatcher {
* 删除按钮的引用
private Drawable mClearD
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);
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的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
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焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length() & 0);
setClearIconVisible(false);
* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
* @param visible
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable :
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
* 当输入框里面内容发生变化的时候回调的方法
public void onTextChanged(CharSequence s, int start, int count,
int after) {
setClearIconVisible(s.length() & 0);
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
public void afterTextChanged(Editable s) {
* 设置晃动动画
public void setShakeAnimation(){
this.setAnimation(shakeAnimation(5));
* 晃动动画
* @param counts 1秒钟晃动多少下
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateA
搜索框颜色选择器:
&span style=&font-size:18&&&?xml version=&1.0& encoding=&UTF-8&?&
&selector xmlns:android=&/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:18&&package com.example.
* 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 StringB
private static CharacterParser characterParser = new CharacterParser();
public static CharacterParser getInstance() {
return characterP
public String getResource() {
public void setResource(String resource) {
this.resource =
/** * 汉字转成ASCII码 * * @param chs * @return */
private int getChsAscii(String chs) {
int asc = 0;
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);
/** * 单字解析 * * @param str * @return */
public String convert(String str) {
String result =
int ascii = getChsAscii(str);
if (ascii & 0 && ascii & 160) {
result = String.valueOf((char) ascii);
for (int i = (pyvalue.length - 1); i &= 0; i--) {
if (pyvalue[i] &= ascii) {
result = pystr[i];
/** * 词组解析 * * @param chs * @return */
public String getSelling(String chs) {
String key,
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&;
buffer.append(value);
return buffer.toString();
public String getSpelling() {
return this.getSelling(this.getResource());
&/span&4. 拼音比较类:PinyinComparator.java
&span style=&font-size:18&&package com.example.
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 o1.getSortLetters().compareTo(o2.getSortLetters());
&/span&5. 显示列表信息的名字以及其拼音首字母 GrouMemberBean.java
&span style=&font-size:18&&package com.example.
public class GroupMemberBean {
//显示的数据
private String sortL
//显示数据拼音的首字母
public String getName() {
public void setName(String name) {
this.name =
public String getSortLetters() {
return sortL
public void setSortLetters(String sortLetters) {
this.sortLetters = sortL
&/span&6.ExpandaleListView 适配器MyExpandableAdapter.java
&span style=&font-size:18&&package com.example.
import java.util.L
import android.content.C
import android.view.LayoutI
import android.view.V
import android.view.View.OnClickL
import android.view.ViewG
import android.widget.BaseExpandableListA
import android.widget.ImageV
import android.widget.LinearL
import android.widget.TextV
import android.widget.T
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Context mC
private List&GroupMemberBean& mG
private List&List&GroupMemberBean&& mC
private LayoutInflater mI
public MyExpandableAdapter(Context mContext,List&GroupMemberBean& mGroup,List&List&GroupMemberBean&& mChild){
this.mContext = mC
this.mGroup = mG
this.mChild = mC
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 = mG
this.mChild = mC
notifyDataSetChanged();
public int getGroupCount() {
// TODO Auto-generated method stub
return mGroup.size();
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mChild.get(groupPosition).size();
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return mGroup.get(groupPosition);
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return mChild.get(groupPosition).get(childPosition);
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupP
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childP
public boolean hasStableIds() {
// TODO Auto-generated method stub
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupHolderView groupHolderV
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);
groupHolderView = (GroupHolderView) convertView.getTag();
groupHolderView.groupTv.setText(mGroup.get(groupPosition).getName());
return convertV
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ChildHolderView childHolderV
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);
childHolderView = (ChildHolderView) convertView.getTag();
childHolderView.childTv.setText(mChild.get(groupPosition).get(childPosition).getName());
childHolderView.childLl.setOnClickListener(new OnClickListener(){
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 convertV
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
class GroupHolderView{
TextView groupTv;
class ChildHolderView{
ImageView childIv;
TextView childTv;
LinearLayout childLl;
GroupHolderView:用于显示组信息
ChildHolderView:用于显示组下成员信息。包括一个LinearLayout,一个ImageView,一个TextVIew
private List&GroupMemberBean& mG 包含组的所有List信息
private List&List&GroupMemberBean&& mC包含所有的组下成员信息
&span style=&font-size:18&& public void updateListView(List&GroupMemberBean& mGroup,List&List&GroupMemberBean&& mChild) {
this.mGroup = mG
this.mChild = mC
notifyDataSetChanged();
}&/span&根据传入的mGroup,mChild来更新列表信息。
7.GroupHolderView类所对应的activity_group.xml
&span style=&font-size:18&&&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
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:18&&&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/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& /&
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:18&&&?xml version=&1.0& encoding=&UTF-8&?&
&selector xmlns:android=&/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:18&&package com.example.
import java.util.ArrayL
import java.util.C
import java.util.L
import android.app.A
import android.os.B
import android.text.E
import android.text.TextU
import android.text.TextW
import android.view.M
import android.view.MenuI
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.ExpandableListV
import android.widget.TextV
public class MainActivity extends Activity {
private Button myB
private ExpandableListView myExpandLv;
private ClearEditText myClearEt;
private TextView noSearchResultTv;
private MyExpandableAdapter myA
private List&GroupMemberBean& groupBeanL
private List&List&GroupMemberBean&& childBeanList = new ArrayList&List&GroupMemberBean&&();
* 汉字转换成拼音的类
private CharacterParser characterP
* 根据拼音来排列ListView里面的数据类
private PinyinComparator pinyinC
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
childBeanList.add(tempTwo);
//用于生成列表信息
myBtn = (Button) this.findViewById(R.id.my_btn);
myBtn.setOnClickListener(new OnClickListener() {
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() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
filterData(s.toString());
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
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 super.onOptionsItemSelected(item);
* 为ListView填充数据
* @param date
private List&GroupMemberBean& filledData(String[] date) {
List&GroupMemberBean& mSortList = new ArrayList&GroupMemberBean&();
for (int i = 0; i & date. 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());
sortModel.setSortLetters(&#&);
mSortList.add(sortModel);
return mSortL
* 根据输入框中的值来过滤数据并更新ListView
* @param filterStr
private void filterData(String filterStr) {
List&GroupMemberBean& groupFilterList = new ArrayList&GroupMemberBean&();
List&GroupMemberBean& tempFilterL
List&List&GroupMemberBean&& childFilterList = new ArrayList&List&GroupMemberBean&&();
if (TextUtils.isEmpty(filterStr)) {
groupFilterList = groupBeanL
childFilterList = childBeanL
noSearchResultTv.setVisibility(View.GONE);
groupFilterList.clear();
childFilterList.clear();
for (int i = 0; i & groupBeanList.size(); i++) {
//标记departGroup是否加入元素
boolean isAddGroup =
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 =
for (int j = 0; j & childBeanList.get(i).size(); j++) {
GroupMemberBean sortChildModel = childBeanList.get(i)
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 =
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);
myExpandLv.collapseGroup(i);
//搜索的结果全部展开
for (int i = 0; i & groupFilterList.size(); i++) {
myExpandLv.expandGroup(i);
//如果查询的结果为0时,显示为搜索到结果的提示
if(groupFilterList.size() == 0){
noSearchResultTv.setVisibility(View.VISIBLE);
noSearchResultTv.setVisibility(View.GONE);
&span style=&font-size:18&&
myBtn.setOnClickListener(new OnClickListener() {
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:18&&private List&GroupMemberBean& filledData(String[] date) &/span&根据 res-&array定义的字符数组,转化为List&GroupMemberBean&
&span style=&font-size:18&&private void filterData(String filterStr) {&/span&根据查询条件更新列表信息,
重点:1.如果查询的条件为空,直接更新列表初始状态;
假如:filterStr = “g”;
& &2.如果查询的结果为空,直接给出“未搜索到结果”提示;
& &3.如果Group中包含filterStr,GroupMemberBean sortModel&直接加入groupFilterList,并标记已加入isAddGroup =
& & & 如果Child中包含filterStr,定义临时的tempFilterList,GroupMemberBean sortChildModel加入到childFilterList,如果groupFilterList不包含其父组,并将其父加入到groupFilterList,并标记已加入isAddGroup=
& & & 最后如果isAddGroup=true,将tempFilterList加入到childFilterList。
& &4.通过myAdapter.updateListView(groupFilterList, childFilterList),更新列表信息。
其上为本博文的所有内容。
源代码下载:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:198544次
积分:2816
积分:2816
排名:第11076名
原创:79篇
转载:48篇
评论:62条
(7)(3)(6)(3)(2)(4)(6)(4)(3)(2)(2)(3)(2)(5)(6)(1)(5)(5)(4)(6)(7)(5)(12)(19)(3)(3)(2)(2)

我要回帖

更多关于 hongniu163 的文章

 

随机推荐