首先,在你的XML布局文件中添加ExpandableListView:
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
接下来,创建一个适配器(Adapter)以提供数据。这个适配器需要实现BaseExpandableListAdapter接口。以下是一个简单的例子:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader; // 标头列表
private HashMap<String, List<String>> listDataChild; // 子项数据
public MyExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listDataChild) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return listDataChild.get(listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = convertView.findViewById(R.id.lblListHeader);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
在上述代码中,R.layout.list_group 和 R.layout.list_item 是用于显示组标题和子项的布局文件。你需要创建这两个布局文件并定义它们的样式。
最后,在你的Activity中使用ExpandableListView:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = findViewById(R.id.expandableListView);
// 准备数据
List<String> listDataHeader = new ArrayList<>();
HashMap<String, List<String>> listDataChild = new HashMap<>();
// 添加组数据
listDataHeader.add("Group 1");
listDataHeader.add("Group 2");
// 添加子项数据
List<String> group1Items = new ArrayList<>();
group1Items.add("Item 1.1");
group1Items.add("Item 1.2");
List<String> group2Items = new ArrayList<>();
group2Items.add("Item 2.1");
group2Items.add("Item 2.2");
listDataChild.put(listDataHeader.get(0), group1Items);
listDataChild.put(listDataHeader.get(1), group2Items);
// 创建适配器
MyExpandableListAdapter listAdapter = new MyExpandableListAdapter(this, listDataHeader, listDataChild);
// 设置适配器
expandableListView.setAdapter(listAdapter);
}
}
在这个例子中,ExpandableListView 显示两个组,每个组有两个子项。你可以根据需要修改数据和布局文件,以满足你的实际需求。
转载请注明出处:http://www.pingtaimeng.com/article/detail/15151/Android