在Android studio中,设计一个ExpandableListView
时间: 2024-01-24 21:48:17 浏览: 138
步骤如下:
1. 在layout文件夹中创建一个xml文件,命名为expandable_list.xml,用于设计ExpandableListView的布局。
2. 在xml文件中添加一个ExpandableListView控件。
3. 在activity中获取该ExpandableListView控件,并为其设置适配器。
4. 创建一个ExpandableListAdapter类,该类继承自BaseExpandableListAdapter,并实现其相关方法。
5. 在ExpandableListAdapter类中重写getGroupCount方法,用于获取组的数量。
6. 在ExpandableListAdapter类中重写getChildrenCount方法,用于获取子项的数量。
7. 在ExpandableListAdapter类中重写getGroupView方法,用于获取组的视图。
8. 在ExpandableListAdapter类中重写getChildView方法,用于获取子项的视图。
9. 在ExpandableListAdapter类中重写getGroup方法,用于获取组的数据。
10. 在ExpandableListAdapter类中重写getChild方法,用于获取子项的数据。
11. 在activity中创建数据源,用于存储组和子项的数据。
12. 在activity中为ExpandableListView设置适配器,将数据源和ExpandableListAdapter关联起来。
13. 运行程序,查看效果。
示例代码如下:
expandable_list.xml:
```xml
<ExpandableListView
android:id="@+id/expandable_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list);
expandableListView = findViewById(R.id.expandable_list);
// 创建数据源
List<String> groupList = new ArrayList<>();
List<List<String>> childList = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
groupList.add("Group " + i);
List<String> childListItem = new ArrayList<>();
for (int j = 1; j <= 5; j++) {
childListItem.add("Child " + j);
}
childList.add(childListItem);
}
// 设置适配器
ExpandableListAdapter adapter = new ExpandableListAdapter(this, groupList, childList);
expandableListView.setAdapter(adapter);
}
}
```
ExpandableListAdapter.java:
```java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> groupList;
private List<List<String>> childList;
public ExpandableListAdapter(Context context, List<String> groupList, List<List<String>> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childList.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) {
TextView textView = new TextView(context);
textView.setText(groupList.get(groupPosition));
textView.setPadding(100, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(childList.get(groupPosition).get(childPosition));
textView.setPadding(150, 0, 0, 0);
textView.setTextSize(18);
textView.setTextColor(Color.GRAY);
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
```
阅读全文