android 下拉树
时间: 2023-06-30 10:26:35 浏览: 149
Android应用源码之全国省市县下拉地址选择源码.zip
在 Android 中实现下拉树可以使用 ExpandableListView 控件。ExpandableListView 是 ListView 的扩展,它可以展开和折叠子项,类似于树形结构。
以下是实现下拉树的基本步骤:
1. 准备数据:定义一个包含父项和子项的数据结构。
2. 创建适配器:继承 BaseExpandableListAdapter 类,实现其方法,将数据绑定到 ExpandableListView 上。
3. 初始化 ExpandableListView:在布局文件中定义 ExpandableListView 控件,并在代码中获取它的实例。
4. 设置适配器:将适配器设置给 ExpandableListView 控件。
5. 添加事件监听器:处理父项和子项的点击事件,实现展开和折叠子项的功能。
下面是一个简单的示例代码:
```
public class MainActivity extends AppCompatActivity {
private ExpandableListView mExpListView;
private List<String> mGroupList;
private Map<String, List<String>> mChildMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
mExpListView = findViewById(R.id.exp_list_view);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, mGroupList, mChildMap);
mExpListView.setAdapter(adapter);
mExpListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// 处理父项点击事件
if (mExpListView.isGroupExpanded(groupPosition)) {
mExpListView.collapseGroup(groupPosition);
} else {
mExpListView.expandGroup(groupPosition);
}
return true;
}
});
mExpListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// 处理子项点击事件
String childText = mChildMap.get(mGroupList.get(groupPosition)).get(childPosition);
Toast.makeText(MainActivity.this, childText, Toast.LENGTH_SHORT).show();
return true;
}
});
}
private void initData() {
mGroupList = new ArrayList<>();
mChildMap = new HashMap<>();
mGroupList.add("水果");
mGroupList.add("蔬菜");
List<String> fruitList = new ArrayList<>();
fruitList.add("苹果");
fruitList.add("香蕉");
fruitList.add("橙子");
mChildMap.put("水果", fruitList);
List<String> vegetableList = new ArrayList<>();
vegetableList.add("青菜");
vegetableList.add("白菜");
vegetableList.add("西红柿");
mChildMap.put("蔬菜", vegetableList);
}
}
```
在布局文件中定义 ExpandableListView 控件:
```
<ExpandableListView
android:id="@+id/exp_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
在适配器中实现数据绑定:
```
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mGroupList;
private Map<String, List<String>> mChildMap;
public MyExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childMap) {
mContext = context;
mGroupList = groupList;
mChildMap = childMap;
}
@Override
public int getGroupCount() {
return mGroupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mChildMap.get(mGroupList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mChildMap.get(mGroupList.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 false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String groupText = mGroupList.get(groupPosition);
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.group_item, parent, false);
}
TextView groupTextView = convertView.findViewById(R.id.group_text_view);
groupTextView.setText(groupText);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = mChildMap.get(mGroupList.get(groupPosition)).get(childPosition);
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.child_item, parent, false);
}
TextView childTextView = convertView.findViewById(R.id.child_text_view);
childTextView.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
```
group_item.xml:
```
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/group_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"/>
```
child_item.xml:
```
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/child_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"/>
```
阅读全文