ExpendableListView 怎么用
时间: 2024-04-18 10:07:53 浏览: 6
ExpendableListView是Android中的一个可扩展的列表视图控件,可以用于显示多层级的数据。使用ExpendableListView需要以下步骤:
1. 创建一个继承自BaseExpandableListAdapter的适配器类,实现以下方法:
- getGroupCount():返回分组的数量。
- getChildrenCount():返回指定分组中子项的数量。
- getGroup():返回指定分组的数据对象。
- getChild():返回指定分组中指定子项的数据对象。
- getGroupView():返回指定分组的视图对象。
- getChildView():返回指定分组中指定子项的视图对象。
- isChildSelectable():指定指定分组中的子项是否可选中。
2. 在布局文件中添加ExpendableListView控件。
3. 在Activity或Fragment中,获取ExpendableListView控件的引用,设置适配器,并设置分组的点击事件监听器。
示例代码:
1. 创建适配器类
```java
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mGroupList;
private List<List<String>> mChildList;
public MyExpandableListAdapter(Context context, List<String> groupList, List<List<String>> childList) {
mContext = context;
mGroupList = groupList;
mChildList = childList;
}
@Override
public int getGroupCount() {
return mGroupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mChildList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mChildList.get(groupPosition).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) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_group, parent, false);
TextView textView = view.findViewById(R.id.tv_group_name);
textView.setText(mGroupList.get(groupPosition));
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_child, parent, false);
TextView textView = view.findViewById(R.id.tv_child_name);
textView.setText(mChildList.get(groupPosition).get(childPosition));
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
```
2. 在布局文件中添加ExpendableListView控件
```xml
<ExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 在Activity或Fragment中,设置ExpendableListView的适配器和分组的点击事件监听器
```java
public class MainActivity extends AppCompatActivity {
private ExpandableListView mExpandableListView;
private List<String> mGroupList;
private List<List<String>> mChildList;
private MyExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandableListView = findViewById(R.id.expandable_list_view);
initData();
mAdapter = new MyExpandableListAdapter(this, mGroupList, mChildList);
mExpandableListView.setAdapter(mAdapter);
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Toast.makeText(MainActivity.this, "点击了分组:" + mGroupList.get(groupPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void initData() {
mGroupList = new ArrayList<>();
mGroupList.add("水果");
mGroupList.add("蔬菜");
mChildList = new ArrayList<>();
List<String> fruitList = new ArrayList<>();
fruitList.add("苹果");
fruitList.add("香蕉");
fruitList.add("橙子");
mChildList.add(fruitList);
List<String> vegetableList = new ArrayList<>();
vegetableList.add("西红柿");
vegetableList.add("黄瓜");
vegetableList.add("茄子");
mChildList.add(vegetableList);
}
}
```