在Android中使用ExpandableListView,首先需要创建一个Adapter来提供数据和定义列表项的布局。然后,在Activity或Fragment中,将ExpandableListView与Adapter进行绑定并设置点击事件。
以下是一个使用ExpandableListView的示例:
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> groupList;
private Map<String, List<String>> childMap;
public ExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childMap) {
this.context = context;
this.groupList = groupList;
this.childMap = childMap;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
String groupName = groupList.get(groupPosition);
return childMap.get(groupName).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String groupName = groupList.get(groupPosition);
return childMap.get(groupName).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_item_layout, null);
}
TextView groupNameTextView = convertView.findViewById(R.id.groupNameTextView);
groupNameTextView.setText(groupList.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item_layout, null);
}
TextView childNameTextView = convertView.findViewById(R.id.childNameTextView);
String groupName = groupList.get(groupPosition);
String childName = childMap.get(groupName).get(childPosition);
childNameTextView.setText(childName);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private ExpandableListAdapter expandableListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.expandableListView);
// 创建分组数据
List<String> groupList = new ArrayList<>();
groupList.add("Group 1");
groupList.add("Group 2");
groupList.add("Group 3");
// 创建子项数据
Map<String, List<String>> childMap = new HashMap<>();
List<String> childList1 = new ArrayList<>();
childList1.add("Child 1-1");
childList1.add("Child 1-2");
childList1.add("Child 1-3");
childMap.put("Group 1", childList1);
List<String> childList2 = new ArrayList<>();
childList2.add("Child 2-1");
childList2.add("Child 2-2");
childMap.put("Group 2", childList2);
List<String> childList3 = new ArrayList<>();
childList3.add("Child 3-1");
childMap.put("Group 3", childList3);
// 创建Adapter
expandableListAdapter = new ExpandableListAdapter(this, groupList, childMap);
// 绑定Adapter
expandableListView.setAdapter(expandableListAdapter);
// 设置点击事件
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
String groupName = (String) expandableListAdapter.getGroup(groupPosition);
String childName = (String) expandableListAdapter.getChild(groupPosition, childPosition);
Toast.makeText(MainActivity.this, "Clicked: " + groupName + " - " + childName, Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
以上代码创建了一个ExpandableListView,其中包含3个分组(Group 1、Group 2、Group 3),每个分组下面有若干子项。点击子项时,会弹出一个Toast显示分组和子项的名称。
注意:在上述代码中,group_item_layout.xml和child_item_layout.xml是分组和子项的布局文件,可以根据实际需求进行自定义。