SimpeExpandableListAdapter实现ExpandableAdapter(上)(安卓开发学习笔记——19)

请添加图片描述
2.
请添加图片描述
3.代码示例:
(1)java代码:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListView;

    String[] groupStringArr = {"腾讯", "百度", "阿里巴巴"};
    String[] [] childStringArrs = {
            {"QQ","微信","QQ浏览器"},
            {"百度搜索","百度地图","百度外卖"},
            {"淘宝","支付宝","天猫"}
    };

    List<Map<String, ?>> groupData = new ArrayList<>();
    List<List<Map<String,?>>> childData = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view);

        for (int i = 0; i < groupStringArr.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("groupName", groupStringArr[i]);

            groupData.add(map);

            List<Map<String,?>> itemList = new ArrayList<>();
            for (int j = 0; j < childStringArrs[i].length; j++) {
                Map<String,String> map1 = new HashMap<>();
                map1.put("itemName",childStringArrs[i][j]);
                itemList.add(map1);

            }

            childData.add(itemList);

        }

                                    /**this: 是参数context,代表当前活动。**/

                                    /**groupData:数据源List<Map<String, ?>>**/

                                    /**R.layout.item_group:(参数:int groupLayout)调用的布局文件**/

                                    /**String[]{"groupName"}:(参数:String[] groupFrom)数据源List<Map<String, ?>>**/

                                    /**int[]{R.id.tv_group}:(int[] groupTo)数据源List<Map<String, ?>>**/
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, groupData,
                R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
                childData,R.layout.item_child,new String[]{"itemName"}, new int[]{R.id.tv_child});

        expandableListView.setAdapter(adapter);

    }
}

(2)布局代码:

//主活动布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/demo_expandable_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ExpandableListView>

</LinearLayout>
//item_child布局(新建)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:paddingLeft="50dp"
    android:layout_height="60dp">

    <TextView
        android:id="@+id/tv_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </TextView>


</LinearLayout>
//item_group布局(新建)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:layout_height="60dp">

    <TextView
        android:id="@+id/tv_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </TextView>
    
</LinearLayout>

4.图片:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值