ExpandableListView如何进行二级扩展(SimpleExpandableListAdapter)

本文详细介绍了如何使用ExpandableListView实现简单的二级扩展功能,包括使用SimpleExpandableListAdapter进行数据展示,以及如何在Fragment中操作二级扩展。此外,文章还讨论了在扩展中遇到的问题及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在网上搜索,发现现在在操作ListView上面,进行二级扩展的是ExpandableListView,但一般会有三种方式进行操作。

一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView
二是 扩展BaseExpandableListAdpter
三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter。

这里面先说一下:SimpleExpandableListAdpater操作

首先我们需要知道一级菜单和二级菜单,一般我们以groups为一级,childs为子级。

先看下,groups.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/textGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="40px"
        android:paddingTop="6px"
        android:paddingBottom="6px"
        android:textSize="25sp"
        android:text="No data"
        />
</LinearLayout>

子级布局文件childs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >   
    <TextView
        android:id="@+id/textChild"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingLeft="60px"
        android:paddingTop="10px"
        android:paddingBottom="10px"
        android:textSize="20sp"
        android:text="No Data"
        android:layout_marginLeft="0dp"/>

</LinearLayout>

显示当前界面的布局文件,我是以steam.xml文件命名的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <ExpandableListView
        android:id="@+id/expandable"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

我本人是在Fragment里面进行操作二级扩展的和普通的activity有点不同,但大同小异。java文件

public class StreamFragment extends Fragment {

    List<Map<String, String>> gruops = new ArrayList<Map<String, String>>();

    List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

    private ExpandableListView ep;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View messageLayout = inflater.inflate(R.layout.stream, container, false);
        ep = (ExpandableListView) messageLayout.findViewById(R.id.expandable);
        setListData();
        return messageLayout;
    }

    /**
     * 设置列表内容
     */
    public void setListData() {
        // 创建一级条目标题
        String[] names ={"1月","2月"};
        //创建二级标题
        String[][]  child_names= {{"费用","衣服","用品"},{"老家", "学校","美丽"}};

        for(int i=0;i<names.length;i++){
            Map<String,String> namedata = new HashMap<String ,String>();
            namedata.put("names",names[i]);
            gruops.add(namedata);

            List<Map<String, String>> child_map = new ArrayList<Map<String, String>>();
            for(int j=0;j<child_names[0].length;j++)
            {
                Map<String,String>  mapcs   = new HashMap<String,String>();
                mapcs.put("child_names",child_names[i][j]);
                child_map.add(mapcs);
//                System.out.println(child_map);
            }

            childs.add(child_map);
//            System.out.println(childs);
        }

        /**
         * 创建ExpandableList的Adapter容器 参数: 1.上下文 2.一级集合 3.一级样式文件 4. 一级条目键值
         * 5.一级显示控件名 6. 二级集合 7. 二级样式 8.二级条目键值 9.二级显示控件名
         *
         */
        SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
                getActivity(), gruops, R.layout.groups, new String[]{"names"},
                new int[]{R.id.textGroup}, childs, R.layout.childs,
                new String[]{"child_names"}, new int[]{R.id.textChild,R.id.imageChild});

        // 加入列表
        ep.setAdapter(sela);
    }

}

这里面可能需要自己补充知识,关于如何在activity里面操作Fragement.
这样就可以做出来一个简单的二级展开。
但是这样我发现子级如果需要添加图片,文字和其他自定义的扩展,这个方法不是很合适,只能做元素只有一个子级。(不知道我说的对不对,希望有新用法的朋友给我补充下)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值