ExpandableListView :就是一种用于垂直滚动展示二级列表的视图,组可以单独展开。这些数据是通过ExpandableListView关联的。
一、使用
1.定义布局文件
<ExpandbaleListView
android:id="@id/expand_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2.定义要显示的数据
//group数据
private ArrayList<String> GroutList;
//item数据
private ArrayList<ArrayList<String>> ItemSet;
private void initData(){
GropList= new ArrayList<>();
GroupList .add("家人");
GroupList.add("朋友");
GroupList.add("同事");
}
ItemSet= new ArrayList<>();
ArrayList<String> People=new ArrayList<>();
people.add(哥哥);
people.add(姐姐);
people.add(弟弟);
ArrayList<String> friend= new ArrayList<>();
friend.add(小红);
friend.add(小宝);
friend.add(晓晓);
ArrayList<String> colleagues=new ArrayList<>();
colleague.add(张三);
colleague.add(李四);
colleague.add(王五);
3.定义组视图和子选项的视图组视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:id="@+id/tv_group"
android:textSize="18sp"
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="4/18"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
子视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_heigth="wrap_content"
android:layout_centerVertaical="true"
android:padding="8dp"
<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 抽象类,重写相关的方法
public class MyAdapter extends BaseExpandableListAdapter{
private Context context;
private ArrayList<String> group;
private ArrayList<ArrayList<String>> ItemList;
private final LayoutInflater inflater;
public MyAdapter(Context context, ArrayList<String> group, ArrayList<ArrayList<String>> itemList){
this.context = context;
this.group = group;
this.ItemList = itemList;
Inflater = LayoutInflater.from(context);
}
//父项的个数
@Override
public int getGroupCount() {
return group.size();
}
//某个父项的子项的个数
@Override
public int getChildrenCount(int groupPosition) {
return ItemList.get(groupPosition).size();
}
//获得某个父项
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
//获得某个子项
@Override
public Object getChild(int groupPosition, int childPosition) {
return ItemList.get(groupPosition).get(childPosition);
}
//父项的Id
@Override
public long getGroupId(int groupPosition){
return groupPosition;
}
//子项的id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//获取父项的view
@Override
public View getGroupView(int
groupPosition, boolean isExpanded, View
convertView, ViewGroup parent) {
if (convertView == null){
convertView = Inflater.inflate(R.layout.item_group,parent,false); }
String group = group.get(groupPosition);
TextView tvgroup = (TextView) convertView.findViewById(R.id.tv_group);
tvgroup.setText(group);
return convertView;
}
//获取子项的view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String child = ItemList.get(groupPosition).get(childPosition);
if (convertView == null){
convertView = Inflater.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;
}
//子项是否可选中,如果要设置子项的点击事件,需要返回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
5.为 ExpandableListView 设置适配器
MyAdapter adapter = new MyAdapter(this, groupList, ItemSet);
mExpandableListView.setAdapter(adapter);