分享Android中ExpandableListView控件使用教程
本文采用一個(gè)Demo來展示Android中ExpandableListView控件的使用,如如何在組/子ListView中綁定數(shù)據(jù)源。直接上代碼如下:
程序結(jié)構(gòu)圖:
layout目錄下的 main.xml 文件源碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 我們會(huì)自己定義listview的顯示方式(在另外一個(gè)布局文件里邊)不用默認(rèn)的方式 如果自定義listview的顯示方式這里這個(gè)android:id="@id/android:list" 必須這樣寫 --> <!-- android:drawSelectOnTop="false"此屬性用來設(shè)置listview上的背景顏色會(huì)不會(huì) 擋?。ǜ采w)內(nèi)容 , 如果這是為false就表示不會(huì)覆蓋掉 --> <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawSelectorOnTop="false"/> </LinearLayout>
包c(diǎn)om.andyidea.demo中ContactsActivity.java源碼如下:
package com.andyidea.demo; import java.util.ArrayList; import java.util.List; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; public class ContactsActivity extends ExpandableListActivity { List<String> group; //組列表 List<List<String>> child; //子列表 ContactsInfoAdapter adapter; //數(shù)據(jù)適配器 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //設(shè)置為無標(biāo)題 setContentView(R.layout.main); getExpandableListView().setBackgroundResource(R.drawable.default_bg); initializeData(); getExpandableListView().setAdapter(new ContactsInfoAdapter()); getExpandableListView().setCacheColorHint(0); //設(shè)置拖動(dòng)列表的時(shí)候防止出現(xiàn)黑色背景 } /** * 初始化組、子列表數(shù)據(jù) */ private void initializeData(){ group = new ArrayList<String>(); child = new ArrayList<List<String>>(); addInfo("Andy",new String[]{"male","138123***","GuangZhou"}); addInfo("Fairy",new String[]{"female","138123***","GuangZhou"}); addInfo("Jerry",new String[]{"male","138123***","ShenZhen"}); addInfo("Tom",new String[]{"female","138123***","ShangHai"}); addInfo("Bill",new String[]{"male","138231***","ZhanJiang"}); } /** * 模擬給組、子列表添加數(shù)據(jù) * @param g-group * @param c-child */ private void addInfo(String g,String[] c){ group.add(g); List<String> childitem = new ArrayList<String>(); for(int i=0;i<c.length;i++){ childitem.add(c[i]); } child.add(childitem); } class ContactsInfoAdapter extends BaseExpandableListAdapter{ //-----------------Child----------------// @Override public Object getChild(int groupPosition, int childPosition) { return child.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String string = child.get(groupPosition).get(childPosition); return getGenericView(string); } //----------------Group----------------// @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getGroupCount() { return group.size(); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String string = group.get(groupPosition); return getGenericView(string); } //創(chuàng)建組/子視圖 public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 40); TextView text = new TextView(ContactsActivity.this); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setText(s); return text; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } } }
最后,程序運(yùn)行后截圖如下:
希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android 關(guān)于ExpandableListView刷新問題的解決方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- Android中ExpandableListView的用法實(shí)例
- android使用ExpandableListView控件實(shí)現(xiàn)小說目錄效果的例子
- Android ExpandableListView展開列表控件使用實(shí)例
- Android ExpandableListView長按事件的完美解決辦法
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android之帶group指示器的ExpandableListView(自寫)
- Android中ExpandableListView使用示例詳解
相關(guān)文章
Android Studio中一套代碼多渠道打包的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Studio中一套代碼多渠道打包的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-05-05Flutter打包apk報(bào)錯(cuò)Your?app?isn't?using?AndroidX解決
這篇文章主要為大家介紹了Flutter打包apk報(bào)錯(cuò)Your?app?isn't?using?AndroidX解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android實(shí)現(xiàn)簡單點(diǎn)贊動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單點(diǎn)贊動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解
這篇文章主要為大家介紹了Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android微信右滑退出功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android微信右滑退出功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Android開發(fā)實(shí)現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法,涉及Android彈出窗口功能、布局及屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-09-09