【android】用 ExpandableListView 来实现 TreeView树形菜单视图

使用 ExpandableListView 来实现 TreeView

创建一个 ExpandableListAdapter 来为其提供数据。以下演示了如何使用 ExpandableListView 来展示树形结构的数据:

首先,在布局文件中添加 ExpandableListView:

xml 复制代码
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

接下来,创建一个 ExpandableListAdapter 类来管理树形结构的数据:

java 复制代码
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groupList; // 父项数据
    private Map<String, List<String>> childMap; // 子项数据

    public MyExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childMap) {
        this.context = context;
        this.groupList = groupList;
        this.childMap = childMap;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childMap.get(groupList.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String childText = (String) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);
        }
        TextView textView = convertView.findViewById(R.id.listItem);
        textView.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childMap.get(groupList.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String groupText = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_group, null);
        }
        TextView textView = convertView.findViewById(R.id.listGroup);
        textView.setText(groupText);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

在上述代码中,我们创建了一个自定义的 ExpandableListAdapter,用于管理父项和子项的数据展示。在 getChildView 和 getGroupView 方法中,我们根据数据来填充父项和子项的视图。

最后,在 Activity 或者 Fragment 中设置 ExpandableListView 的适配器并提供数据:

java 复制代码
ExpandableListView expandableListView = findViewById(R.id.expandableListView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(getContext(), groupList, childMap); // groupList 和 childMap 是你的数据
expandableListView.setAdapter(adapter);

如果你想在 ExpandableListView 中实现第一级为名称,第二级为名称+logo的效果,你可以修改 MyExpandableListAdapter 类的 getChildView 方法来实现这个需求。以下是一个简单的示例代码,展示了如何为第二级添加名称和 logo:

java 复制代码
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    String childText = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }
    TextView textView = convertView.findViewById(R.id.listItem);
    ImageView imageView = convertView.findViewById(R.id.listLogo); // 假设你在布局文件中有一个 ImageView 用于显示 logo
    textView.setText(childText);
    
    // 设置对应的 logo
    if (childText.equals("名称1")) {
        imageView.setImageResource(R.drawable.logo1);
    } else if (childText.equals("名称2")) {
        imageView.setImageResource(R.drawable.logo2);
    }
    
    return convertView;
}

在上述代码中,我们修改了 getChildView 方法,为每个子项设置了对应的 logo。假设你在布局文件中有一个 ImageView 用于显示 logo,我们根据子项的名称来设置对应的 logo。

这样,当你设置 ExpandableListView 的适配器后,每个第二级子项将会显示名称和对应的 logo。

布局文件

以下是一个简单的布局文件示例,用于在 ExpandableListView 中展示第一级为名称,第二级为名称+logo的效果:

list_group.xml(用于展示第一级名称):

xml 复制代码
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:textSize="20sp"
    android:textColor="@android:color/black"/>

list_item.xml(用于展示第二级名称和logo):

xml 复制代码
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <TextView
        android:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@android:color/black"/>

    <ImageView
        android:id="@+id/listLogo"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginStart="16dp"/>
</LinearLayout>
相关推荐
夜晚中的人海1 小时前
【C++】智能指针介绍
android·java·c++
用户2018792831671 小时前
后台Activity输入分发超时ANR分析(无焦点窗口)
android
用户2018792831671 小时前
Activity配置变化后ViewModel 的 “不死之谜”
android
游戏开发爱好者82 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_916008892 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915106324 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
2501_915921435 小时前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式
齊家治國平天下6 小时前
Android 14 Input 事件派发机制深度剖析
android·input·hal
2501_916013747 小时前
iOS 推送开发完整指南,APNs 配置、证书申请、远程推送实现与上架调试经验分享
android·ios·小程序·https·uni-app·iphone·webview
李艺为9 小时前
非预置应用使用platform签名并且添加了android.uid.system无法adb安装解决方法
android·adb