Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
SimpleExpandableListAdapter
is a helper class in Android used for creating an expandable list view backed by simple arrays and Maps. The expandable list has groups (parent items) and children (sub-items under each parent).
Here's a simple example to show how to use SimpleExpandableListAdapter
:
activity_main.xml (Layout with the ExpandableListView
):
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent" />
group_item.xml (Layout for each group/parent):
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/groupText" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:textSize="18sp" />
child_item.xml (Layout for each child):
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/childText" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="32dp" android:paddingTop="8dp" android:paddingBottom="8dp" android:textSize="16sp" />
In your MainActivity
:
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Sample Data val groups = listOf( mapOf("GROUP_NAME" to "Fruits"), mapOf("GROUP_NAME" to "Animals") ) val children = listOf( listOf( mapOf("CHILD_NAME" to "Apple"), mapOf("CHILD_NAME" to "Banana") ), listOf( mapOf("CHILD_NAME" to "Cat"), mapOf("CHILD_NAME" to "Dog") ) ) val adapter = SimpleExpandableListAdapter( this, groups, R.layout.group_item, arrayOf("GROUP_NAME"), intArrayOf(R.id.groupText), children, R.layout.child_item, arrayOf("CHILD_NAME"), intArrayOf(R.id.childText) ) val expandableListView: ExpandableListView = findViewById(R.id.expandableListView) expandableListView.setAdapter(adapter) } }
In the above code:
groups
: A list of maps, each representing a group. The key is the column name (e.g., "GROUP_NAME"), and the value is the actual data.
children
: A list of lists of maps. The outer list corresponds to the groups, and the inner lists represent the children of those groups.
SimpleExpandableListAdapter
parameters:
groupData
and view IDs to bind the data.childData
and view IDs to bind the data.This example provides a basic usage of SimpleExpandableListAdapter
. Depending on the requirements, more advanced customizations might be needed, for which creating a custom adapter would be the right approach.
Implementing SimpleExpandableListAdapter in Android example code:
// Define group and child data List<Map<String, String>> groupData = new ArrayList<>(); List<List<Map<String, String>>> childData = new ArrayList<>(); // Set group and child data to the adapter SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, android.R.layout.simple_expandable_list_item_1, new String[] { "GroupTitle" }, new int[] { android.R.id.text1 }, childData, android.R.layout.simple_expandable_list_item_2, new String[] { "ChildTitle" }, new int[] { android.R.id.text1 } ); // Set adapter to ExpandableListView ExpandableListView expandableListView = findViewById(R.id.expandableListView); expandableListView.setAdapter(adapter);
Using SimpleExpandableListAdapter with ExpandableListView in Android:
<ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/>
Customizing SimpleExpandableListAdapter layout in Android:
Description: Customizes the layout used by SimpleExpandableListAdapter for both group and child items.
Example Code (XML):
<!-- Custom layout for group items --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/customGroupText" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <!-- Custom layout for child items --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/customChildText" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
// Specify custom view IDs int[] groupTo = {R.id.customGroupText}; int[] childTo = {R.id.customChildText}; // Create SimpleExpandableListAdapter with custom layouts SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, R.layout.custom_group_layout, new String[] { "GroupTitle" }, groupTo, childData, R.layout.custom_child_layout, new String[] { "ChildTitle" }, childTo );
Handling data binding with SimpleExpandableListAdapter in Android:
// Use SimpleExpandableListAdapter.ViewBinder for data binding SimpleExpandableListAdapter.ViewBinder viewBinder = (groupPosition, isExpanded, convertView, parent) -> { // Handle data binding for group items if (groupPosition % 2 == 0) { // Customize the appearance of even group items // Example: convertView.setBackgroundColor(Color.YELLOW); return true; } return false; }; // Set the view binder to the SimpleExpandableListAdapter adapter.setViewBinder(viewBinder);
Adding images to SimpleExpandableListAdapter in Android:
// Add image data to the map groupItem.put("GroupImage", R.drawable.group_icon); childItem.put("ChildImage", R.drawable.child_icon); // Specify additional keys for the images String[] groupFrom = { "GroupTitle", "GroupImage" }; String[] childFrom = { "ChildTitle", "ChildImage" }; // Specify additional view IDs for the images int[] groupTo = { R.id.text1, R.id.groupImageView }; int[] childTo = { R.id.text1, R.id.childImageView };
Creating a SimpleExpandableListAdapter for grouped data in Android:
// Set data for groups and children List<Map<String, String>> groupData = new ArrayList<>(); List<List<Map<String, String>>> childData = new ArrayList<>(); // Set data to the adapter SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, android.R.layout.simple_expandable_list_item_1, new String[] { "GroupTitle" }, new int[] { android.R.id.text1 }, childData, android.R.layout.simple_expandable_list_item_2, new String[] { "ChildTitle" }, new int[] { android.R.id.text1 } );
SimpleExpandableListAdapter with multiple data sets in Android:
// Define multiple data sets for groups and children List<Map<String, String>> groupData1 = new ArrayList<>(); List<Map<String, String>> groupData2 = new ArrayList<>(); List<List<Map<String, String>>> childData1 = new ArrayList<>(); List<List<Map<String, String>>> childData2 = new ArrayList<>(); // Set multiple data sets to the adapter List<List<Map<String, String>>> allChildData = new ArrayList<>(); allChildData.add(childData1); allChildData.add(childData2); SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData1, android.R.layout.simple_expandable_list_item_1, new String[] { "GroupTitle" }, new int[] { android.R.id.text1 }, allChildData, android.R.layout.simple_expandable_list_item_2, new String[] { "ChildTitle" }, new int[] { android.R.id.text1 } );
Dynamic data loading with SimpleExpandableListAdapter in Android:
// Clear existing data groupData.clear(); childData.clear(); // Add new data Map<String, String> newGroupItem = new HashMap<>(); newGroupItem.put("GroupTitle", "New Group"); groupData.add(newGroupItem); // Notify adapter of data change adapter.notifyDataSetChanged();
Updating data in SimpleExpandableListAdapter in Android:
// Modify existing data Map<String, String> groupItemToUpdate = groupData.get(0); groupItemToUpdate.put("GroupTitle", "Updated Group"); // Notify adapter of data change adapter.notifyDataSetChanged();
Expanding and collapsing groups with SimpleExpandableListAdapter in Android:
// Expand a group expandableListView.expandGroup(groupPosition); // Collapse a group expandableListView.collapseGroup(groupPosition);
Styling SimpleExpandableListAdapter items in Android:
Description: Applies styles to SimpleExpandableListAdapter items for a visually appealing interface.
Example Code (XML):
<!-- Apply styles to the custom layout or default layout --> <style name="CustomText"> <item name="android:textSize">18sp</item> <item name="android:textColor">#FF0000</item> </style>
<!-- Apply styles to TextView in the custom layout --> <TextView android:id="@+id/customGroupText" style="@style/CustomText" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Alternative to SimpleExpandableListAdapter in Android:
// Explore using custom adapters or RecyclerView for more complex scenarios
Handling group and child click events with SimpleExpandableListAdapter:
// Set group click listener expandableListView.setOnGroupClickListener((parent, v, groupPosition, id) -> { // Handle group item click // Return true if you want to prevent the group from being expanded/collapsed return false; }); // Set child click listener expandableListView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> { // Handle child item click return true; });