在實現類似通信錄,等帶有兩層或多層組織架構的列表功能時,會使用到ExpandableListView,他是ListView的子類,使用方式也和ListView大同小異,這里做一個總結。
創新互聯公司長期為上1000+客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為路北企業提供專業的成都網站設計、成都網站制作,路北網站改版等技術服務。擁有10余年豐富建站經驗和眾多成功案例,為您定制開發。關鍵類:
1、SimpleExpandableListAdapter
2、SimpleCursorTreeAdapter
3、BaseExpandableListAdapter
4、ExpandableListActivity
SimpleExpandableListAdapter
構造方法:
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, //組數據
int groupLayout,//組數據使用的布局文件
String[] groupFrom, //組數據所使用的key數組
int[] groupTo,//組數據放置的控件id,例如TextView
List<? extends List<? extends Map<String,?>>> childData,//子元素數據
int childLayout, //子元素使用的布局文件
String[] childFrom, //子元素數據所使用的key數組
int[] childTo)//子元素數據放置的控件id,例如TextView
例如:生成一條組數據,組名為“group1”
Map<String, String> map1 = new HashMap<>();
map1.put("group", "group1");//其中"group"只是一個索引key
集合保存所有的組數據
List<Map<String, String>> groups = new ArrayList<>();
保存一條子元素數據,和組數據一樣
Map<String, String> cMap1 = new HashMap<>();
cMap1.put("child","child1_data1");
child1.add(cMap1);//添加到集合
保存一個組的子元素數據
List<Map<String, String>> child1 = new ArrayList<>();
保存所有組的子元素數據
List<List<Map<String, String>>> childs = new ArrayList<>();
構造方法
SimpleExpandableListAdapter adapter1 = new SimpleExpandableListAdapter(this,
groups, R.layout.expandable_group, new String[]{"group"}, new int[]{R.id.text_group},
childs, R.layout.expandable_child, new String[]{"child"}, new int[]{R.id.text_child});
BaseExpandableListAdapter
繼承BaseExpandableListAdapter實現自己的Adapter:
public class MyAdapter extends BaseExpandableListAdapter{ String[] groups = new String[]{"Group_111","Group_222","Group_333"}; String[][] childs = new String[][]{ {"g1_c1","g1_c2","g1_c3"}, {"g2_c1","g2_c2","g2_c3"}, {"g3_c1","g3_c2","g3_c3"}}; @Override public int getGroupCount() { return groups.length; } @Override public int getChildrenCount(int groupPosition) { return childs[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return childs[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition;//這里直接返回組元素下標 } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition;//這里直接返回第二層元素下標 } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.expandable_group, null); TextView text = (TextView)view.findViewById(R.id.text_group); text.setText(groups[groupPosition]); return view;//實際項目中應該考慮convertView的重用 } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.expandable_child, null); TextView text = (TextView)view.findViewById(R.id.text_child); text.setText(childs[groupPosition][childPosition]); return view; } @Override//組合子元素是否擁有穩定的id,底層的數據集改表是否影響該id public boolean hasStableIds() { return false; } @Override//子元素是否可選中,有點擊反饋 public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
ExpandableListActivity和ListActivity類似
寫自己的Activity繼承自該類
該類自帶一個ExpandableListView。id為:“@android:id/list”(或者代碼中為“list”)
可以有一個id為:“@android:id/empty”布局,作為adapter沒數據時的替代布局
通常寫自己的布局并滿足上述條件,并加以擴展實現自己的需求
SimpleCursorTreeAdapter
不同于SimpleExpandableListAdapter數據源來自List<Map<?,?>>,SimpleCursorTreeAdapter數據源來自訪問數據庫返回的Cursor。
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文標題:ExpandableListView總結-創新互聯
標題URL:http://vcdvsql.cn/article28/cecicp.html
成都網站建設公司_創新互聯,為您提供定制開發、網站制作、響應式網站、關鍵詞優化、Google、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯