Android中ExpandableListView中巢狀ListView
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 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;
}
}
總結一下,大概就這幾步,實現起來雖然不然麻煩,但是程式碼也挺多的。
就這些了。
相關文章
- android中的ListViewAndroidView
- ScrollView巢狀ListView解決辦法以及原理View巢狀
- repeater中巢狀放入RadioButtonList巢狀
- Vue中的巢狀路由Vue巢狀路由
- 從原理上說說ScrollView巢狀ListView的問題View巢狀
- sql server中巢狀事務*SQLServer巢狀
- JavaScript中if巢狀assert的方法JavaScript巢狀
- 解決Flutter的ListView巢狀ListView滑動衝突以及無限高度問題FlutterView巢狀
- Android ListView 進階——從列表中獲取值AndroidView
- Linux的中斷可以巢狀嗎?Linux巢狀
- Android:巢狀滑動總結Android巢狀
- 對比SQL中簡單巢狀查詢與非巢狀查詢CFSQL巢狀
- iOS block巢狀block中weakify的使用iOSBloC巢狀
- es中如何使用巢狀物件查詢巢狀物件
- Android實現雙層ViewPager巢狀AndroidViewpager巢狀
- 【docker巢狀】在docker中的linux中再安裝dockerDocker巢狀Linux
- 【BUG系列】Fragment中巢狀Fragment,不顯示Fragment巢狀
- js中的函式巢狀和閉包JS函式巢狀
- SQL Server中存在真正的“事務巢狀”SQLServer巢狀
- android開發中如何動態獲取listview中的item的值AndroidView
- Android巢狀滑動邏輯淺析Android巢狀
- Android實現RecyclerView巢狀流式佈局AndroidView巢狀
- ExpandableListViewView
- Android ListView中複雜資料流的高效渲染(一)AndroidView
- 減少程式碼中該死的 if else 巢狀巢狀
- setTimeout和箭頭函式巢狀中的this指向函式巢狀
- HTML中div巢狀div的margin不起作用HTML巢狀
- 舉例說明表格中如何再巢狀表格巢狀
- 【java】【集合】去除ArrayList中的元素、ArrayList巢狀ArrayListJava巢狀
- Mysql中的巢狀子查詢問題QSBSMySql巢狀
- Flutter 中 ListView 的使用FlutterView
- 類與介面(三)java中的介面與巢狀介面Java巢狀
- java中如何將巢狀迴圈效能提高500倍Java巢狀
- 【Android ViewPager】解決ViewPager巢狀時在API 13及其以下版本中不能滑動的問題AndroidViewpager巢狀API
- Android中Fragment巢狀Fragment,切換Fragment時不顯示檢視的原因及解決方法AndroidFragment巢狀
- 迴圈中巢狀非同步操作的流程控制巢狀非同步
- Go中struct巢狀與物件導向以及上下文GoStruct巢狀物件
- android 設定listview高度AndroidView
- 讀取 jar 包中巢狀的 jar 包內容的方法JAR巢狀