目录
3.1 setAdapter(ExpandableListAdapter adapter)
3.4 setOnGroupCollapseListener
1.ExpandableListView概述
1.1 定义
ExpandableListView继承自ListView,主要由组与子元素组成。是一种用于垂直滚动展示两级列表的视图,所以我们要分别对组元素以及子元素进行配置及操作。
1.2 与ListView的区别
ExpandableListView 和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。这个 ExpandableListAdapter 又是什么呢?和 ListView 使用的 BaseAdapter 差不多,都是用来给 View 提供数据、 实例化子布局的。实际使用的时候实现这个接口就可以了。
1.3 应用场景
需要对数据进行分组展示,如联系人分组等。如下图所示:点击销售目录,然后展开其下面的三个子item目录。
2.常用属性
2.1 groupIndicator
分组指示器,可以设置父item元素在展开和收缩显示不同的图标,如下所示:
<ExpandableListView ... android:groupIndicator="@drawable/xxxxx" ... />
有默认的图标,可以自己自定义配置。
2.2 childIndicator
为子item添加图标,默认不显示。如下所示:
<ExpandableListView ... android:childIndicator="@drawable/xxxxx" ... />
2.3 childDivider
为子item设置分割线的颜色,如下所示:
<ExpandableListView ... android:childDivider="@colore/xxxxx" ... />
3.常用的方法
3.1 setAdapter(ExpandableListAdapter adapter)
用于设置数据适配器。
3.2 setOnGroupClickListener
用于为父item设置监听事件 。
3.2 setOnChildClickListener
用于为子item设置监听事件 。
3.4 setOnGroupCollapseListener
为父item设置收缩监听事件。
3.5 setOnGroupExpandListener
为父item设置展开监听事件。
4.案例展示
4.1 说明
分组展示数据。效果图如下图所示:
4.2 创建数据源
public class ChapterLab { public static List<Chapter> getChapterList(){ List<Chapter> chapterList = new ArrayList<>(); Chapter c1 = new Chapter(1,"Android"); Chapter c2 = new Chapter(2,"IOS"); Chapter c3 = new Chapter(3,"Unity 3D"); Chapter c4 = new Chapter(4,"Java"); c1.addChildItem(1,"Android Child 1"); c1.addChildItem(2,"Android Child 1"); c1.addChildItem(3,"Android Child 1"); c1.addChildItem(4,"Android Child 1"); c1.addChildItem(5,"Android Child 1"); c2.addChildItem(1,"IOS Child 1"); c2.addChildItem(2,"IOS Child 2"); c2.addChildItem(3,"IOS Child 3"); c2.addChildItem(4,"IOS Child 4"); c3.addChildItem(1,"Unity 3D Child 1"); c3.addChildItem(2,"Unity 3D Child 2"); c3.addChildItem(3,"Unity 3D Child 3"); c3.addChildItem(4,"Unity 3D Child 4"); c4.addChildItem(1,"Java Child 1"); c4.addChildItem(2,"Java Child 2"); c4.addChildItem(3,"Java Child 3"); c4.addChildItem(4,"Java Child 4"); chapterList.add(c1); chapterList.add(c2); chapterList.add(c3); chapterList.add(c4); return chapterList; } }
4.3 创建布局视图
1)主布局: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"> <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent" android:childDivider="#fc06a2"/> </LinearLayout>
2)父item布局:item_parent_chapter.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff"> <ImageView android:id="@+id/imageview" android:layout_width="28dp" android:layout_height="28dp" android:layout_gravity="center_vertical" tools:background="@drawable/indicator_coolapse" android:layout_marginRight="5dp"/> <TextView android:id="@+id/id_parent_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="Android" android:textSize="20sp" android:textColor="#000000" android:layout_gravity="center_vertical" /> </LinearLayout>
3)子item布局:item_child_chapter.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f6e0a1"> <TextView android:id="@+id/id_child_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_gravity="center_vertical" android:textColor="#1b1b1b" tool:text=""/> </LinearLayout>
3)设置selector:group_indicator.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/indicator_expand" android:state_selected="true"/> <item android:drawable="@drawable/indicator_coolapse"/> </selector>
4.4 设置适配器:
/** * 适配器类 */ public class ChapterAdapter extends BaseExpandableListAdapter{ private List<Chapter> chapterList; private LayoutInflater inflater; private Context context; public ChapterAdapter(Context context,List<Chapter> chapterList) { this.chapterList = chapterList; this.context = context; inflater = LayoutInflater.from(context); } //返回组item数量 @Override public int getGroupCount() { return chapterList.size(); } //返回对应下标的组item下面的所有子item数量 @Override public int getChildrenCount(int groupPosition) { return chapterList.get(groupPosition).getChildItemList().size(); } //根据下标返回对应的组item @Override public Object getGroup(int groupPosition) { return chapterList.get(groupPosition); } //返回指定下标的子item @Override public Object getChild(int groupPosition, int childPosition) { return chapterList.get(groupPosition).getChildItemList().get(childPosition); } //返回指定下标的组item的id @Override public long getGroupId(int groupPosition) { return groupPosition; } //返回指定下标的子item的id @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } //加载组item的视图,isExpanded表示是否处于展开状态 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ParentViewHolder parentViewHolder = null; if(convertView == null){ convertView = inflater.inflate(R.layout.item_parent_chapter,parent,false); parentViewHolder = new ParentViewHolder(); parentViewHolder.parentTextView = convertView.findViewById(R.id.id_parent_tv); parentViewHolder.imageView = convertView.findViewById(R.id.imageview); convertView.setTag(parentViewHolder); }else{ parentViewHolder = (ParentViewHolder)convertView.getTag(); } Chapter chapter = chapterList.get(groupPosition); parentViewHolder.parentTextView.setText(chapter.getName()); parentViewHolder.imageView.setImageResource(R.drawable.group_indicator); //设置图标是否展开 parentViewHolder.imageView.setSelected(isExpanded); return convertView; } //加载子item的视图 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder = null; if(convertView == null){ convertView = inflater.inflate(R.layout.item_child_chapter,parent,false); childViewHolder = new ChildViewHolder(); childViewHolder.childTextView = convertView.findViewById(R.id.id_child_tv); convertView.setTag(childViewHolder); }else{ childViewHolder = (ChildViewHolder)convertView.getTag(); } ChapterItem chapterItem = chapterList.get(groupPosition).getChildItemList().get(childPosition); childViewHolder.childTextView.setText(chapterItem.getName()); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public static class ParentViewHolder{ TextView parentTextView; ImageView imageView; } public static class ChildViewHolder{ TextView childTextView; } }
4.5 MainActivity.java
public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private BaseExpandableListAdapter adapter; private List<Chapter> chapterList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ expandableListView = findViewById(R.id.expandableListView); chapterList.clear(); chapterList.addAll(ChapterLab.getChapterList()); adapter = new ChapterAdapter(this,chapterList); expandableListView.setAdapter(adapter); } }
去掉分割线
android:divider="@null" android:dividerHeight="0dp"
去掉父item图标
// ExpandableListView的布局的id
ExpandableListView.setGroupIndicator(null);
ExpandableListView默认打开所有子布局
//设置默认子布局全部打开 listview_lv是ExpandableListView的布局的id
int groupCount = listview_lv.getCount();
for (int i=0; i<groupCount; i++) {
listview_lv.expandGroup(i);
}
//listview_lv.expandGroup(0);如果默认打开第一组 就设置这个
LogUtil.e("adapter",mlist.size()+"---------");
listview_lv.setAdapter(adapter);
//设置父布局没有点击事件
listview_lv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;//这里return true 是父布局没有点击事件
}
});