版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/51136868
最近项目挺紧张,一直没有时间总结学习,今天把最近遇到的一个奇葩的设计,做一下总结。其他的好多的APP中做的通讯录都类似微信通讯录这样,但是有这样一个需求的设计。
就是分为两个组,第一个组不需要A-Z的索引,第二组需要A-Z的索引。以下是我的实现思路。
1.实现思路
1.写布局文件,将ExpandableListView添加到布局文件。
2.写MyExpandableListViewAdapter的实现
3.ListView设置为不可滑动。
4.再第一组中,添加一个ListView,第二个组中,添加一个ListView根据用户名的拼音实现A-Z的排序展示。
2.代码的实现
2.1 主文件布局文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".view.ExtenpListViewActivity">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/elv_good_friend"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
2.2 MyExpandableListViewAdapter的实现
/**
*@类名 : ExtenpListViewActivity
*@描述 :
*@时间 : 2016/4/12 13:56
*@作者: 李东
*@邮箱 : lidong@chni.com.cn
*@company: chni
*/
private class MyExpandableListViewAdapter extends BaseExpandableListAdapter{
public MyExpandableListViewAdapter(Context context) {
this.context = context;
}
private Context context;
@Override
public int getGroupCount() {
return mGroup.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;//注意返回值必须为1,不然会重复
}
@Override
public Object getGroup(int groupPosition) {
return mGroup[groupPosition] ;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mGroupData[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
int count = 0;
count = mGroupData[groupPosition].length;
return count;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView text = (TextView) View.inflate(context, android.R.layout.simple_expandable_list_item_1, null);
text.setText(mGroup[groupPosition]);
return text;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (groupPosition==0){
SubListView lv = (SubListView) View.inflate(context, R.layout.item_expend_listview, null);
final List<Friend> mSourceDateList = mMap.get("我的好友");
ListViewAdaAdapter1 listViewAdaAdapter = new ListViewAdaAdapter1(mSourceDateList, context);
lv.setAdapter(listViewAdaAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ExtenpListViewActivity.this, mSourceDateList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
return lv;
}else{
SubListView lv = (SubListView) View.inflate(context, R.layout.item_expend_listview, null);
final List<Friend> mSourceDateList = mMap.get("我的关注");
Log.d(TAG, "mSourceDateList: " + mSourceDateList.size());
List<Friend> SourceDateList = new ArrayList<>();
Log.d(TAG, "getChildView() called with: " + "groupPosition = [" + groupPosition + "], childPosition = [" + childPosition + "], convertView = [" + convertView + "],数据" + mSourceDateList.size());
SourceDateList= filledData(mSourceDateList);
// 根据a-z进行排序源数据
Collections.sort(SourceDateList, pinyinComparator);
ListViewAdaAdapter listViewAdaAdapter = new ListViewAdaAdapter(SourceDateList, context);
lv.setAdapter(listViewAdaAdapter);
final List<Friend> finalSourceDateList = SourceDateList;
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ExtenpListViewActivity.this, finalSourceDateList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
return lv;
}
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
注意:在这里做的过程遇到一个坑,就是ListView的元素的重复,最后百度查了一下,需要做如下设置:
@Override
public int getChildrenCount(int groupPosition) {
return 1;//注意返回值必须为1,不然会重复
}
2.3 ListView设置为不可滑动
package com.lidong.demo.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
*@类名 : SubListView
*@描述 :
*@时间 : 2016/4/12 14:54
*@作者: 李东
*@邮箱 : lidong@chni.com.cn
*@company: chni
*/
public class SubListView extends ListView {
public SubListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
2.4 PinyinComparator 拼音比较方法
package com.lidong.demo.view.fragment;
import java.util.Comparator;
/**
* Created by lidongon 2016/4/12.
*/
public class PinyinComparator implements Comparator<Friend> {
public static PinyinComparator instance = null;
public static PinyinComparator getInstance() {
if (instance == null) {
instance = new PinyinComparator();
}
return instance;
}
public int compare(Friend o1, Friend o2) {
if (o1.getLetters().equals("@")
|| o2.getLetters().equals("#")) {
return -1;
} else if (o1.getLetters().equals("#")
|| o2.getLetters().equals("@")) {
return 1;
} else {
return o1.getLetters().compareTo(o2.getLetters());
}
}
}
2.5 Java汉字转换为拼音
/*
* Filename CharacterParser.java
* Company Chni
* @author lidong
* @version 0.1
*/
package com.lidong.demo.view.fragment;
/**
* 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;
if (chs == null) {
return asc;
}
try {
byte[] bytes = chs.getBytes("gb2312");
if (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 = convert(key);
if (value == null) {
value = "unknown";
}
} else {
value = key;
}
buffer.append(value);
}
return buffer.toString();
}
public String getSpelling() {
return this.getSelling(this.getResource());
}
}
2.6实体类:
package com.lidong.demo.view.fragment;
/**
* Created by lidong on 2016/4/12.
*/
public class Friend {
String name; //名字
String letters; // 字母
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLetters() {
return letters;
}
public void setLetters(String letters) {
this.letters = letters;
}
}
3 界面的代码:
package com.lidong.demo.view;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import com.lidong.demo.R;
import com.lidong.demo.view.fragment.CharacterParser;
import com.lidong.demo.view.fragment.Friend;
import com.lidong.demo.view.fragment.PinyinComparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
public class ExtenpListViewActivity extends AppCompatActivity {
@Bind(R.id.elv_good_friend)
ExpandableListView elvGoodFriend;
MyExpandableListViewAdapter mMyExpandableListViewAdapter;
/**
* 组的数据
*/
private static final String[] mGroup = new String[] {"我的好友","我的关注"};
/**
* 每个组的数据
*/
private String[][] mGroupData = new String[][] {
{ "王银1", "王银2", "王银3"},
{ "李1", "王1", "李2", "王2", "李3", "王3"}};
private String TAG = ExtenpListViewActivity.class.getSimpleName();
private Map<String,List<Friend>> mMap = new HashMap<>();
private int mFlag = 1;
/**
* 根据拼音来排列ListView里面的数据类
*/
private PinyinComparator pinyinComparator;
/**
* 汉字转换成拼音的类
*/
private CharacterParser characterParser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cicle);
ButterKnife.bind(this);
initData();
pinyinComparator = PinyinComparator.getInstance();
characterParser = CharacterParser.getInstance();
mMyExpandableListViewAdapter = new MyExpandableListViewAdapter(this);
elvGoodFriend.setAdapter(mMyExpandableListViewAdapter);
elvGoodFriend.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.d(TAG, "onChildClick() called with: " + "parent = [" + parent + "], v = [" + v + "], groupPosition = [" + groupPosition + "], childPosition = [" + childPosition + "], id = [" + id + "]");
return false;
}
});
}
private void initData() {
List<Friend> list = new ArrayList<>();
Friend f1 = new Friend();
f1.setName("王银1");
list.add(f1);
mMap.put("我的好友",list);
List<Friend> list2 = new ArrayList<>();
Friend f2 = new Friend();
f2.setName("王银2");
list2.add(f2);
Friend f3 = new Friend();
f3.setName("李2");
list2.add(f3);
Friend f4 = new Friend();
f4.setName("胡3");
list2.add(f4);
mMap.put("我的关注",list2);
Log.d(TAG, "initData() called with: " + mMap.get("我的好友").toString()+"--"+mMap.get("我的关注").toString());
}
/**
*@类名 : ExtenpListViewActivity
*@描述 :
*@时间 : 2016/4/12 13:56
*@作者: 李东
*@邮箱 : lidong@chni.com.cn
*@company: chni
*/
private class MyExpandableListViewAdapter extends BaseExpandableListAdapter{
public MyExpandableListViewAdapter(Context context) {
this.context = context;
}
private Context context;
@Override
public int getGroupCount() {
return mGroup.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;//注意返回值必须为1,不然会重复
}
@Override
public Object getGroup(int groupPosition) {
return mGroup[groupPosition] ;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mGroupData[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
int count = 0;
count = mGroupData[groupPosition].length;
return count;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView text = (TextView) View.inflate(context, android.R.layout.simple_expandable_list_item_1, null);
text.setText(mGroup[groupPosition]);
return text;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (groupPosition==0){
SubListView lv = (SubListView) View.inflate(context, R.layout.item_expend_listview, null);
final List<Friend> mSourceDateList = mMap.get("我的好友");
ListViewAdaAdapter1 listViewAdaAdapter = new ListViewAdaAdapter1(mSourceDateList, context);
lv.setAdapter(listViewAdaAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ExtenpListViewActivity.this, mSourceDateList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
return lv;
}else{
SubListView lv = (SubListView) View.inflate(context, R.layout.item_expend_listview, null);
final List<Friend> mSourceDateList = mMap.get("我的关注");
Log.d(TAG, "mSourceDateList: " + mSourceDateList.size());
List<Friend> SourceDateList = new ArrayList<>();
Log.d(TAG, "getChildView() called with: " + "groupPosition = [" + groupPosition + "], childPosition = [" + childPosition + "], convertView = [" + convertView + "],数据" + mSourceDateList.size());
SourceDateList= filledData(mSourceDateList);
// 根据a-z进行排序源数据
Collections.sort(SourceDateList, pinyinComparator);
ListViewAdaAdapter listViewAdaAdapter = new ListViewAdaAdapter(SourceDateList, context);
lv.setAdapter(listViewAdaAdapter);
final List<Friend> finalSourceDateList = SourceDateList;
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ExtenpListViewActivity.this, finalSourceDateList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
return lv;
}
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
private class ListViewAdaAdapter1 extends BaseAdapter{
List<Friend> mName;
Context mContext;
public ListViewAdaAdapter1( List<Friend> name, Context context) {
mName = name;
mContext = context;
}
@Override
public int getCount() {
return mName.size();
}
@Override
public Object getItem(int position) {
return mName.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =null;
if (convertView ==null) {
holder = new ViewHolder();
convertView = View.inflate(mContext, R.layout.item_listview_item1, null);
holder.mTextView = (TextView) convertView.findViewById(R.id.tv_text);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Log.d(TAG, "getView() called with: " + "position = [" + position + "], 数据"+mName.get(position).getName());
holder.mTextView.setText(mName.get(position).getName());
return convertView;
}
private class ViewHolder {
TextView mTextView;
}
}
private class ListViewAdaAdapter extends BaseAdapter{
List<Friend> mName;
Context mContext;
public ListViewAdaAdapter( List<Friend> name, Context context) {
mName = name;
mContext = context;
}
@Override
public int getCount() {
return mName.size();
}
@Override
public Object getItem(int position) {
return mName.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =null;
if (convertView ==null) {
holder = new ViewHolder();
convertView = View.inflate(mContext, R.layout.item_listview_item, null);
holder.mTextView = (TextView) convertView.findViewById(R.id.tv_text);
holder.mCatalog = (TextView) convertView.findViewById(R.id.catalog);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Log.d(TAG, "getView() called with: " + "position = [" + position + "], 数据"+mName.get(position).getName());
holder.mTextView.setText(mName.get(position).getName());
holder.mCatalog.setText(mName.get(position).getLetters());
return convertView;
}
private class ViewHolder {
TextView mTextView,mCatalog;
}
}
/**
* 为ListView填充数据
*
* @param
* @return
*/
private List<Friend> filledData(List<Friend> lsit) {
List<Friend> mFriendList = new ArrayList<>();
for (int i = 0; i < lsit.size(); i++) {
Friend friendModel = new Friend();
friendModel.setName(lsit.get(i).getName());
//汉字转换成拼音
String pinyin = characterParser.getSelling(lsit.get(i).getName());
String sortString = pinyin.substring(0, 1).toUpperCase();
// 正则表达式,判断首字母是否是英文字母
if (sortString.matches("[A-Z]")) {
friendModel.setLetters(sortString.toUpperCase());
} else {
friendModel.setLetters("#");
}
mFriendList.add(friendModel);
}
return mFriendList;
}
}
总结一下,大概就这几步,实现起来虽然不然麻烦,但是代码也挺多的。
就这些了。