承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

Android中expandablelistview如何使用

管理员 2023-09-18 12:27:13 网站建设 0 ℃ 0 评论 5589字 收藏

Android中expandablelistview如何使用

Android中expandablelistview如何使用

在Android中使用ExpandableListView,首先需要创建一个Adapter来提供数据和定义列表项的布局。然后,在Activity或Fragment中,将ExpandableListView与Adapter进行绑定并设置点击事件。
以下是一个使用ExpandableListView的示例:
1. 创建一个ExpandableListView的布局文件(例如expandable_list_layout.xml):
```xml
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
2. 创建一个Adapter类来提供数据和定义列表项的布局(例如ExpandableListAdapter.java):
```java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List groupList;
private Map> childMap;
public ExpandableListAdapter(Context context, List groupList, Map> 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;
}
}
```
3. 在Activity或Fragment中,将ExpandableListView与Adapter进行绑定并设置点击事件(例如MainActivity.java):
```java
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 groupList = new ArrayList<>();
groupList.add("Group 1");
groupList.add("Group 2");
groupList.add("Group 3");
// 创建子项数据
Map> childMap = new HashMap<>();
List childList1 = new ArrayList<>();
childList1.add("Child 1⑴");
childList1.add("Child 1⑵");
childList1.add("Child 1⑶");
childMap.put("Group 1", childList1);
List childList2 = new ArrayList<>();
childList2.add("Child 2⑴");
childList2.add("Child 2⑵");
childMap.put("Group 2", childList2);
List childList3 = new ArrayList<>();
childList3.add("Child 3⑴");
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是分组和子项的布局文件,可以根据实际需求进行自定义。

文章来源:丸子建站

文章标题:Android中expandablelistview如何使用

https://www.wanzijz.com/view/84091.html

相关文章

Related articles

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信