參考自Android小白
http://blog.csdn.net/sysukehan/article/details/51960473
MainActivity 的布局 layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Hello World!"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/expandlistview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/updateData"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:textSize="20dp"
android:layout_gravity="center"
android:text="刷新數(shù)據(jù)"/>
</LinearLayout>
</LinearLayout>
<!--需要使分割線消失的話仇让,不要將這兩個(gè)屬性設(shè)置為@null典奉,-->
<!--把它們的顏色設(shè)置為與背景顏色相同的顏色即可。-->
<!--如果想要用自己的圖片替換掉那個(gè)箭頭可以這樣寫
android:groupIndicator="@drawable/picture"
android:groupIndicator="@null"
childIndicator
用于設(shè)置子項(xiàng)前顯示的圖標(biāo)丧叽,不設(shè)置的話默認(rèn)是沒有圖標(biāo)的
-->
父項(xiàng)的布局文件 layout/parent_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/parent_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
子類的布局文件 layout/child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/child_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
具體實(shí)現(xiàn)代碼如下:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
ExpandableListView expandview;
Map<String,List<String>> listMap = new HashMap<>();
String[] parentList = new String[]{"first","second","third"};
List<String> childList1 = new ArrayList<>();
List<String> childList2 = new ArrayList<>();
List<String> childList3 = new ArrayList<>();
private Button button;
BaseExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandview = (ExpandableListView) findViewById(R.id.expandlistview);
button = (Button) findViewById(R.id.updateData);
//設(shè)置childList的數(shù)據(jù)
childList1.add(parentList[0]+"first");
childList1.add(parentList[1]+"first");
childList1.add(parentList[2]+"first");
childList2.add(parentList[0]+"first");
childList2.add(parentList[1]+"first");
childList2.add(parentList[2]+"first");
childList3.add(parentList[0]+"first");
childList3.add(parentList[1]+"first");
childList3.add(parentList[2]+"first");
listMap.put(parentList[0],childList1);
listMap.put(parentList[1],childList2);
listMap.put(parentList[2],childList3);
//設(shè)置適配器
adapter = new MyExpandableListAdapter(this);
expandview.setAdapter(adapter);
// 這里在每個(gè)子項(xiàng)被點(diǎn)擊了之后會(huì)顯示是哪個(gè)子項(xiàng)被點(diǎn)擊了
// 特別注意
//(1)在使用這個(gè)方法的時(shí)候需要將自定義的adapter中的isChildSelectable方法的返回值設(shè)置為true卫玖,
// 否則子項(xiàng)的點(diǎn)擊不生效,但子項(xiàng)布局中設(shè)置的控件的監(jiān)聽器依然可以生效踊淳。
// // 子項(xiàng)是否可選中假瞬,如果需要設(shè)置子項(xiàng)的點(diǎn)擊事件,需要返回true
// @Override
// public boolean isChildSelectable(int i, int i1) {
// return true;
// }
//(2)如果在子項(xiàng)中對(duì)某個(gè)控件設(shè)置了監(jiān)聽器迂尝,這個(gè)控件要注意不能鋪滿整個(gè)子項(xiàng)笨触,
// 所以在設(shè)置高度和寬度時(shí)要特別注意,否則設(shè)置了子項(xiàng)的監(jiān)聽器也是沒有用的
expandview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view,
int parentPos, int childPos, long l) {
Toast.makeText(MainActivity.this,
listMap.get(parentList[parentPos]).get(childPos), Toast.LENGTH_SHORT).show();
return true;
}
});
// 然后設(shè)置ExpandableListView長(zhǎng)按item的監(jiān)聽器:
expandview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
String content = "";
if ((int) view.getTag(R.layout.child_item) == -1) {
content = "父類第" + view.getTag(R.layout.parent_item) + "項(xiàng)" + "被長(zhǎng)按了";
} else {
content = "父類第" + view.getTag(R.layout.parent_item) + "項(xiàng)" + "中的"
+ "子類第" + view.getTag(R.layout.child_item) + "項(xiàng)" + "被長(zhǎng)按了";
}
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
return true;
}
});
//設(shè)置listview的展開和伸縮
expandview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this, "第" + groupPosition + "個(gè)列表伸展了", Toast.LENGTH_SHORT).show();
}
});
expandview.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(MainActivity.this, "第" + groupPosition + "個(gè)列表收縮了", Toast.LENGTH_SHORT).show();
}
});
// 如果需要進(jìn)入的時(shí)候列表就展開雹舀,然后不再收起芦劣,可以這樣設(shè)置:
// 《1》在setAdapter之后遍歷每一個(gè)列表使它們展開
// 《2》然后設(shè)置父類的監(jiān)聽器直接返回true即可,不可以設(shè)置父類的監(jiān)聽器為null说榆,
// 那樣起不到屏蔽原先系統(tǒng)設(shè)置的監(jiān)聽器的效果
for (int i = 0; i < parentList.length; i++){
if (!expandview.isGroupExpanded(i)) {
expandview.expandGroup(i);
}
}
expandview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
// 用于判斷列表是否展開的方法
// if (expandview.isGroupExpanded(i)) {//列表已展開虚吟,返回true;列表未展開签财,返回false
// expandview.collapseGroup(i);
// } else {
//// 如果把這個(gè)參數(shù)設(shè)置為true串慰,列表展開的時(shí)候會(huì)有動(dòng)畫效果,該方法需在API大于等于14的時(shí)候才可以用
// expandview.expandGroup(i, true);
// }
return true;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateData();
Toast.makeText(MainActivity.this, "數(shù)據(jù)已更新", Toast.LENGTH_SHORT).show();
}
});
}
private void updateData() {
childList1.clear();
childList1.add(parentList[0] + "-new-" + "first");
childList1.add(parentList[0] + "-new-" + "second");
childList1.add(parentList[0] + "-new-" + "third");
childList2.clear();
childList2.add(parentList[1] + "-new-" + "first");
childList2.add(parentList[1] + "-new-" + "second");
childList2.add(parentList[1] + "-new-" + "third");
childList3.clear();
childList3.add(parentList[2] + "-new-" + "first");
childList3.add(parentList[2] + "-new-" + "second");
childList3.add(parentList[2] + "-new-" + "third");
//刷新適配器
adapter.notifyDataSetChanged();
}
private class MyExpandableListAdapter extends BaseExpandableListAdapter {
Context mcontext;
LayoutInflater inflater;
public MyExpandableListAdapter(Context context) {
mcontext = context;
inflater = LayoutInflater.from(context);
}
//獲取父項(xiàng)的數(shù)據(jù)
@Override
public int getGroupCount() {
return listMap.size();
}
//獲取子項(xiàng)的數(shù)據(jù)
@Override
public int getChildrenCount(int groupPosition) {
return listMap.get(parentList[groupPosition]).size();
}
//獲取某個(gè)父項(xiàng)
@Override
public Object getGroup(int groupPosition) {
return listMap.get(parentList[groupPosition]);
}
//獲取某個(gè)父項(xiàng)的某個(gè)子項(xiàng)
@Override
public Object getChild(int groupPosition, int childPosition) {
return listMap.get(parentList[groupPosition]).get(childPosition);
}
//獲取某個(gè)父項(xiàng)的id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//獲取某個(gè)子項(xiàng)的id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// 按函數(shù)的名字來理解應(yīng)該是是否具有穩(wěn)定的id唱蒸,這個(gè)方法目前一直都是返回false邦鲫,沒有去改動(dòng)過
@Override
public boolean hasStableIds() {
return false;
}
// 當(dāng)expandlistview的getGroupView或者getChildView中包含checkbox時(shí),前者點(diǎn)擊不可用。
// 解決辦法 在etGroupView或者getChildView的checkbox中添加
// android:clickable="true"
// android:focusable="false"
// android:focusableInTouchMode="false"
// 獲得父項(xiàng)顯示的view
// ExpandableListView 的 數(shù)據(jù)適配器 ExpandableListAdapter 中的
// getGroupView 函數(shù)中所引入的自定義一級(jí)目錄xml 布局文件不能帶有 button庆捺,
// 否則會(huì)導(dǎo)致展開失效古今,ImageButton沒嘗試過,不過可能也是不行的滔以。
// 這里用到了view的setTag方法捉腥,一共設(shè)置了兩個(gè)Tag,
// 標(biāo)簽雖然在設(shè)置的時(shí)候提示說只要int類型即可你画,但一開始使用0和1來做tag的時(shí)候抵碟,
// 顯示沒有報(bào)錯(cuò),但編譯運(yùn)行就報(bào)錯(cuò)了坏匪,要求是資源文件的id才行拟逮,
// 因此換成了R.layout.parent_item和R.layout.child_item。
// 如果是父項(xiàng)适滓,就設(shè)置R.layout.parent_item為第幾個(gè)父項(xiàng)敦迄,
// 設(shè)置R.layout.child_item為-1。如果是子項(xiàng)粒竖,就設(shè)置R.layout.parent_item屬于第幾個(gè)父項(xiàng)
// 設(shè)置R.layout.child_item為該父項(xiàng)的第幾個(gè)子項(xiàng)颅崩,這樣就可以區(qū)分被長(zhǎng)按的是父項(xiàng)還是子項(xiàng)
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_item, null);
}
convertView.setTag(R.layout.parent_item, groupPosition);
convertView.setTag(R.layout.child_item, -1);
TextView text = (TextView) convertView.findViewById(R.id.parent_title);
text.setText(parentList[groupPosition]);
return convertView;
}
// 獲得子項(xiàng)顯示的view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = inflater.inflate(R.layout.child_item, null);
}
convertView.setTag(R.layout.parent_item, groupPosition);
convertView.setTag(R.layout.child_item, childPosition);
TextView text = (TextView) convertView.findViewById(R.id.child_title);
text.setText(listMap.get(parentList[groupPosition]).get(childPosition));
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mcontext, "點(diǎn)到了內(nèi)置的textview", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
// 子項(xiàng)是否可選中几于,如果需要設(shè)置子項(xiàng)的點(diǎn)擊事件蕊苗,需要返回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}