1.代码示例:
(1)Java代码:
//public int getGroupCount()---获取可扩展的组的数量
//public int getChildCount(int groupPosition)---获取每个分组的子元素的个数;int groupPosition---(组的序号)包含多少个子选项
//public Object getGroup(int groupPosition)---返回分组的标题的数据模型;int groupPosition---(组的序号)
//public Object getChild(int groupPosition, int childPosition)---返回指定的组的位置的数据模型; int groupPosition---(当前组的序号),int childPosition---组内子元素的序号
//public long getGroupId(int groupPosition)---返回指定组的ID()
//public long getChildId(int geoupPosition, int childPosition)---返回指定组的指定位置的ID
/**public boolean hasStableIds()---判断public long getGroupId()和public long getChildId()返回的ID是否稳定
当数据模型不变时,返回的ID也不变**/
/**public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)---
生成标题头部的View;convertView: 与缓存有关;parent:它的值就是可扩展的listview**/
/**public View getChildView(int groupPosition, boolean isLastChild, View convertView, ViewGroup parent)---
**/
package com.example.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import org.w3c.dom.Text;
public class SecondActivity extends Activity {
private ExpandableListView expan;
private MyDemoBase adapter;
String[] groupStringArr = {"腾讯", "百度", "阿里巴巴"};
String[] [] childStringArr = {
{"QQ","微信","QQ浏览器"},
{"百度搜索","百度地图","百度外卖"},
{"淘宝","支付宝","天猫"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expan = (ExpandableListView) findViewById(R.id.demo_expandable_list_view);
adapter = new MyDemoBase();
expan.setAdapter(adapter);
}
class MyDemoBase extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return groupStringArr.length; /**组的个数**/
}
@Override
public int getChildrenCount(int groupPosition) {
return childStringArr[groupPosition].length; /**子元素的个数**/
}
@Override
public Object getGroup(int groupPosition) {
return groupStringArr[groupPosition]; /**组的数据模型**/
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childStringArr[groupPosition] [childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition * 100;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 100 + childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group,null);
TextView textView = (TextView) view.findViewById(R.id.tv_group);
Object data = getGroup(groupPosition);
textView.setText((String)data);
if (groupPosition % 2 ==1) {
textView.setTextColor(Color.RED);
}else {
textView.setTextColor(Color.GREEN);
}
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View childView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_child,null);
TextView textView = childView.findViewById(R.id.tv_child);
Object childData = getChild(groupPosition, childPosition);
textView.setText((String) childData);
if (childPosition > 2) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);
}else {
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
}
return childView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
(2)布局代码:
//主活动布局(同一个布局可以被不同的Java文件使用):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/demo_expandable_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
//item_group布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="60dp">
<TextView
android:id="@+id/tv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
3.图片: