1.概述
ExpandableListView是可擴(kuò)展的二級(jí)下拉列表,繼承ListView
2.使用步驟
① 布局中定義ExpandableListView控件
② 初始化Group數(shù)據(jù),Child數(shù)據(jù)(ArrayList<ArrayList<Info>>)
③ 適配器BaseExpandaListAdapter
重寫10個(gè)方法 getGroupCount(),getGroup(),getGroupId(),getGroupView()
getChildCount(),getChild(),getChildId(),getChildView()
hasStablelds()行是否具有唯一id isChildSelectable() 子列表是否可點(diǎn)擊事件
3.屬性
divider 父類分割線
childDivider 子類分割線
dividerHeight 分割線高度
groupIndicator 控制父類前面的小箭頭
android:groupIndicator="@drawable/picture" 箭頭換照片
android:groupIndicator="@null" 去掉小箭頭
indicatorLeft 箭頭或者自己設(shè)置的圖片的右邊框距離手機(jī)左邊邊緣的距離,類似于marginLeft
indicatorStart 箭頭或者自己設(shè)置的圖片的左邊框距離手機(jī)左邊邊緣的距離,類似于marginLeft以上兩個(gè)屬性相結(jié)合可以使箭頭或自己設(shè)置的圖片變大變小或者翻轉(zhuǎn),因?yàn)榭刂屏思^或圖片的左右邊框
childIndicator 用于設(shè)置子項(xiàng)前顯示的圖標(biāo),不設(shè)置的話默認(rèn)是沒有圖標(biāo)的
//設(shè)置圖標(biāo)選擇
<item android:drawable=”@drawable/icon_t” android:state_expanded=”true”/>
<item android:drawable=”@drawable/icon_f” android:state_expanded=”false”/>
代碼
布局
<ExpandableListView
android:id="@+id/expand_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
要顯示的內(nèi)容
//group數(shù)據(jù)
private ArrayList<String> mGroupList;
//item數(shù)據(jù)
private ArrayList<ArrayList<String>> mItemSet;
private void initData() {
mGroupList = new ArrayList<>();
mGroupList.add("我的家人");
mGroupList.add("我的朋友");
mGroupList.add("黑名單");
mItemSet = new ArrayList<>();
ArrayList<String> itemList1 = new ArrayList<>();
itemList1.add("大妹");
itemList1.add("二妹");
itemList1.add("三妹");
ArrayList<String> itemList2 = new ArrayList<>();
itemList2.add("大美");
itemList2.add("二美");
itemList2.add("三美");
ArrayList<String> itemList3 = new ArrayList<>();
itemList3.add("狗蛋");
itemList3.add("二丫");
mItemSet.add(itemList1);
mItemSet.add(itemList2);
mItemSet.add(itemList3);
}
子類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_centerVertical="true"
android:padding="8dp"
android:layout_height="wrap_content">
<ImageView
android:layout_marginLeft="40dp"
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_marginLeft="12dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name"
android:text="@string/app_name"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/app_name"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
4.自定義適配器,需要繼承 BaseExpandableListAdapter 抽象類,重寫相關(guān)的方法
public class MyAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<String> mGroup;
private ArrayList<ArrayList<String>> mItemList;
private final LayoutInflater mInflater;
public MyAdapter(Context context, ArrayList<String> group, ArrayList<ArrayList<String>> itemList){
this.mContext = context;
this.mGroup = group;
this.mItemList = itemList;
mInflater = LayoutInflater.from(context);
}
//父項(xiàng)的個(gè)數(shù)
@Override
public int getGroupCount() {
return mGroup.size();
}
//某個(gè)父項(xiàng)的子項(xiàng)的個(gè)數(shù)
@Override
public int getChildrenCount(int groupPosition) {
return mItemList.get(groupPosition).size();
}
//獲得某個(gè)父項(xiàng)
@Override
public Object getGroup(int groupPosition) {
return mGroup.get(groupPosition);
}
//獲得某個(gè)子項(xiàng)
@Override
public Object getChild(int groupPosition, int childPosition) {
return mItemList.get(groupPosition).get(childPosition);
}
//父項(xiàng)的Id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//子項(xiàng)的id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//獲取父項(xiàng)的view
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = mInflater.inflate(R.layout.item_group,parent,false);
}
String group = mGroup.get(groupPosition);
TextView tvGroup = (TextView) convertView.findViewById(R.id.tv_group);
tvGroup.setText(group);
return convertView;
}
//獲取子項(xiàng)的view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String child = mItemList.get(groupPosition).get(childPosition);
if (convertView == null){
convertView = mInflater.inflate(R.layout.item_item,parent,false);
}
TextView tvChild = (TextView)convertView.findViewById(R.id.tv_name);
tvChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,child,Toast.LENGTH_SHORT).show();
}
});
tvChild.setText(child);
return convertView;
}
//子項(xiàng)是否可選中,如果要設(shè)置子項(xiàng)的點(diǎn)擊事件,需要返回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
5.為 ExpandableListView 設(shè)置適配器
MyAdapter adapter = new MyAdapter(this, mGroupList, mItemSet);
mExpandableListView.setAdapter(adapter);
效果圖
[圖片上傳失敗...(image-a1caa2-1550577205126)]
監(jiān)聽事件
對(duì)于處理 Item 的點(diǎn)擊事件,還是要設(shè)置監(jiān)聽器,常用的有這幾類:
setOnChildClickListener//單擊子選項(xiàng)
setOnGroupClickListener//單擊組選項(xiàng)
setOnGroupCollapseListener//分組合并
setOnGroupExpandListener//分組合并