Ce sera un mémo de la liste qui peut être réduit.
string.xml
<resources>
    <string name="app_name">ExpandableListView</string>
</resources>
activity_main.xml
<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/exListView"></ExpandableListView>
</LinearLayout>
MainActivity.java
package com.websarva.wings.android.expandablelistview;
import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
    List<Map<String, String>> parentList;
    List<List<Map<String, String>>> childList;
    List<Map<String, String>> childDataList;
    Map<String, String> childtData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parentList = new ArrayList<>();
        childList = new ArrayList<>();
        //Générer du contenu à afficher dans la liste parent
        setParentList("parentKey", "Parent 1");
        setParentList("parentKey", "Parent 2");
        //Générer du contenu à afficher dans la liste des enfants
        String firstItemName1[] = {"Enfant 1", "Enfant 2"};
        String secondItemName1[] = {"Enfant 1ère étape", "Deuxième étape enfant"};
        setChildDataList("childKey1", firstItemName1, "childKey2", secondItemName1);
        String firstItemName2[] = {"Enfant A", "Enfant B", "Enfant C"};
        String secondItemName2[] = {"Enfant A étape", "Stade enfant B", "Stade C enfant"};
        setChildDataList("childKey1", firstItemName2, "childKey2", secondItemName2);
        //Création d'adaptateur
        SimpleExpandableListAdapter adapter =
                new SimpleExpandableListAdapter(
                        this,
                        parentList,
                        android.R.layout.simple_expandable_list_item_1,
                        new String[]{"parentKey"},
                        new int[]{android.R.id.text1},
                        childList,
                        android.R.layout.simple_expandable_list_item_2,
                        new String[]{"childKey1", "childKey2"},
                        new int[]{android.R.id.text1, android.R.id.text2}
                );
        ExpandableListView elv = findViewById(R.id.exListView);
        elv.setAdapter(adapter);
    }
    /**
     *Processus de génération du contenu à afficher dans la liste parent
     * @param parentKey
     * @param value
     * @return parentList
     */
    private List<Map<String, String>> setParentList(String parentKey, String value) {
        Map<String, String> parentData = new HashMap<String, String>();
        parentData.put(parentKey, value);
        parentList.add(parentData);
        return parentList;
    }
    /**
     *Processus de génération du contenu à afficher dans la liste enfant
     * @param childKey1
     * @param firstItemName
     * @param childKey2
     * @param secondItemName
     * @return childDataList
     */
    private List<Map<String, String>> setChildData(String childKey1, String firstItemName, String childKey2, String secondItemName) {
        childtData = new HashMap<String, String>();
        childtData.put(childKey1, firstItemName);
        childtData.put(childKey2, secondItemName);
        childDataList.add(childtData);
        return childDataList;
    }
    /**
     *Processus pour créer autant de listes enfants que celles affichées
     * @param childKey1
     * @param firstItemName
     * @param childKey2
     * @param secondItemName
     * @return childList
     */
    private List<List<Map<String, String>>> setChildDataList(String childKey1, String firstItemName[], String childKey2, String secondItemName[]) {
        childDataList = new ArrayList<>();
        //Répétez autant de fois que le nombre d'éléments à créer
        for (int i = 0; i < firstItemName.length; i++) {
            setChildData(childKey1, firstItemName[i], childKey2, secondItemName[i]);
        }
        childList.add(childDataList);
        return childList;
    }
}

Utilisez SimpleExpandableListAdapter pour définir les données que vous souhaitez afficher sous forme de liste dans ExpandableListView. Suivez les étapes ci-dessous pour stocker les données que vous souhaitez afficher sous forme de liste dans SimpleExpandableListAdapter.
Créez autant de listes parentes que d'appels à la méthode setParentList. En tant qu'argument, transmettez la clé et la valeur à stocker dans MAP.
setParentList.Stockage des données parentales
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parentList = new ArrayList<>();
     ~réduction~
        //Générer du contenu à afficher dans la liste parent
        setParentList("parentKey", "Parent 1");
        setParentList("parentKey", "Parent 2");
     ~réduction~
    }
private List<Map<String, String>> setParentList(String parentKey, String value) {
        Map<String, String> parentData = new HashMap<String, String>();
        parentData.put(parentKey, value);
        parentList.add(parentData);
        return parentList;
    }
Créez autant de listes parentes que d'appels à la méthode setChildDataList. En tant qu'argument, passez la clé (1er et 3ème arguments) et la valeur (2ème et 4ème arguments) à stocker dans MAP.
setChildDataList.Stockage des données enfants
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     ~réduction~
        
        childList = new ArrayList<>();
     ~réduction~
        //Générer du contenu à afficher dans la liste des enfants
        String firstItemName1[] = {"Enfant 1", "Enfant 2"};
        String secondItemName1[] = {"Enfant 1ère étape", "Deuxième étape enfant"};
        setChildDataList("childKey1", firstItemName1, "childKey2", secondItemName1);
        String firstItemName2[] = {"Enfant A", "Enfant B", "Enfant C"};
        String secondItemName2[] = {"Enfant A étape", "Stade enfant B", "Stade C enfant"};
        setChildDataList("childKey1", firstItemName2, "childKey2", secondItemName2);
     ~réduction~
     }
private List<Map<String, String>> setChildData(String childKey1, String firstItemName, String childKey2, String secondItemName) {
        childtData = new HashMap<String, String>();
        childtData.put(childKey1, firstItemName);
        childtData.put(childKey2, secondItemName);
        childDataList.add(childtData);
        return childDataList;
    }
    private List<List<Map<String, String>>> setChildDataList(String childKey1, String firstItemName[], String childKey2, String secondItemName[]) {
        childDataList = new ArrayList<>();
        //Répétez autant de fois que le nombre d'éléments à créer
        for (int i = 0; i < firstItemName.length; i++) {
            setChildData(childKey1, firstItemName[i], childKey2, secondItemName[i]);
        }
        childList.add(childDataList);
        return childList;
    }
Stocker les données parent et enfant dans l'adaptateur
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     ~réduction~
        //Création d'adaptateur
        SimpleExpandableListAdapter adapter =
                new SimpleExpandableListAdapter(
                        this,・ ・ ・ ①
                        parentList,・ ・ ・ ②
                        android.R.layout.simple_expandable_list_item_1,・ ・ ・ ③
                        new String[]{"parentKey"},・ ・ ・ ④
                        new int[]{android.R.id.text1},・ ・ ・ ⑤
                        childList,・ ・ ・ ②
                        android.R.layout.simple_expandable_list_item_2,・ ・ ・ ③
                        new String[]{"childKey1", "childKey2"},・ ・ ・ ④
                        new int[]{android.R.id.text1, android.R.id.text2}・ ・ ・ ⑤
                );
        ExpandableListView elv = findViewById(R.id.exListView);
        elv.setAdapter(adapter);
    }
① Contexte auquel ExpandableListView est associé ②List ③ Valeur R représentant la disposition de chaque ligne ④ Touche MAP ⑤ ID TextView qui affiche les caractères dans la mise en page
Voici comment utiliser ExpandableListView.
Recommended Posts