It will be a memo of the list that can be collapsed.
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<>();
//Generate what to display in the parent list
setParentList("parentKey", "Parent 1");
setParentList("parentKey", "Parent 2");
//Generate content to display in child list
String firstItemName1[] = {"Child 1", "Child 2"};
String secondItemName1[] = {"Child 1st stage", "Child 2nd stage"};
setChildDataList("childKey1", firstItemName1, "childKey2", secondItemName1);
String firstItemName2[] = {"Child A", "Child B", "Child C"};
String secondItemName2[] = {"Child A stage", "Child B stage", "Child C stage"};
setChildDataList("childKey1", firstItemName2, "childKey2", secondItemName2);
//Adapter creation
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);
}
/**
*Process to generate the contents to be displayed in the parent list
* @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;
}
/**
*Process to generate the contents to be displayed in the child list
* @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;
}
/**
*Process to create as many child lists as displayed
* @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<>();
//Repeat as many times as the number of items to create
for (int i = 0; i < firstItemName.length; i++) {
setChildData(childKey1, firstItemName[i], childKey2, secondItemName[i]);
}
childList.add(childDataList);
return childList;
}
}
Use the SimpleExpandableListAdapter to set the data you want to display as a list in the ExpandableListView. To store the data you want to display as a list in SimpleExpandableListAdapter, create it according to the following procedure.
Create as many parent lists as there are calls to the setParentList method. As an argument, pass the key and value to be stored in MAP.
setParentList.Storage of parent data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentList = new ArrayList<>();
~abridgement~
//Generate what to display in the parent list
setParentList("parentKey", "Parent 1");
setParentList("parentKey", "Parent 2");
~abridgement~
}
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;
}
Create as many parent lists as there are calls to the setChildDataList method. As arguments, pass the key (1st and 3rd arguments) and value (2nd and 4th arguments) to be stored in MAP.
setChildDataList.Storage of child data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
~abridgement~
childList = new ArrayList<>();
~abridgement~
//Generate content to display in child list
String firstItemName1[] = {"Child 1", "Child 2"};
String secondItemName1[] = {"Child 1st stage", "Child 2nd stage"};
setChildDataList("childKey1", firstItemName1, "childKey2", secondItemName1);
String firstItemName2[] = {"Child A", "Child B", "Child C"};
String secondItemName2[] = {"Child A stage", "Child B stage", "Child C stage"};
setChildDataList("childKey1", firstItemName2, "childKey2", secondItemName2);
~abridgement~
}
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<>();
//Repeat as many times as the number of items to create
for (int i = 0; i < firstItemName.length; i++) {
setChildData(childKey1, firstItemName[i], childKey2, secondItemName[i]);
}
childList.add(childDataList);
return childList;
}
Store parent and child data in the adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
~abridgement~
//Adapter creation
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);
}
① Context to which ExpandableListView is associated ②List ③ R value representing the layout of each line ④ MAP key ⑤ ID of TextView that displays characters in the layout
This is how to use ExpandableListView.
Recommended Posts