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
BaseExpandableListAdapter
is an abstract class in Android that facilitates creating an ExpandableListView
where data is organized in groups and children. Each group can be expanded or collapsed to show or hide its children.
To use the BaseExpandableListAdapter
, you must subclass it and implement its methods to provide data for groups and children.
Let's create a simple ExpandableListView
with countries as groups and their cities as children.
res/layout/activity_main.xml
):<ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/>
Create a custom adapter by extending BaseExpandableListAdapter
:
class CustomExpandableListAdapter( private val context: Context, private val groupList: List<String>, private val childList: HashMap<String, List<String>> ) : BaseExpandableListAdapter() { override fun getGroupCount(): Int { return groupList.size } override fun getChildrenCount(groupPosition: Int): Int { return childList[groupList[groupPosition]]?.size ?: 0 } override fun getGroup(groupPosition: Int): Any { return groupList[groupPosition] } override fun getChild(groupPosition: Int, childPosition: Int): Any { return childList[groupList[groupPosition]]!![childPosition] } override fun getGroupId(groupPosition: Int): Long { return groupPosition.toLong() } override fun getChildId(groupPosition: Int, childPosition: Int): Long { return childPosition.toLong() } override fun hasStableIds(): Boolean { return false } override fun getGroupView( groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup? ): View { var view = convertView if (view == null) { val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater view = inflater.inflate(android.R.layout.simple_expandable_list_item_1, null) } (view as TextView).text = getGroup(groupPosition) as String return view } override fun getChildView( groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup? ): View { var view = convertView if (view == null) { val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater view = inflater.inflate(android.R.layout.simple_list_item_1, null) } (view as TextView).text = getChild(groupPosition, childPosition) as String return view } override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean { return true } }
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val groupList = listOf("USA", "Canada") val childList = hashMapOf( "USA" to listOf("New York", "Los Angeles", "Chicago"), "Canada" to listOf("Toronto", "Vancouver", "Montreal") ) val adapter = CustomExpandableListAdapter(this, groupList, childList) val expandableListView: ExpandableListView = findViewById(R.id.expandableListView) expandableListView.setAdapter(adapter) } }
In this example, the ExpandableListView
displays countries (USA and Canada) as groups. When a group is clicked, it shows the cities within that country.
This is a basic example. For more customized displays, you can design custom layouts for the group and child views and inflate them in the getGroupView
and getChildView
methods, respectively.
Using BaseExpandableListAdapter in Android Kotlin:
BaseExpandableListAdapter
is an abstract class that facilitates the implementation of adapters for ExpandableListViews in Android. It provides methods to handle data and view binding for groups and children.class MyExpandableListAdapter : BaseExpandableListAdapter() { // Implement required methods for groups and children }
ExpandableListView with BaseExpandableListAdapter example:
ExpandableListView
with a custom BaseExpandableListAdapter
.val expandableListView = findViewById<ExpandableListView>(R.id.expandableListView) val adapter = MyExpandableListAdapter() expandableListView.setAdapter(adapter)
Customizing BaseExpandableListAdapter in Android:
BaseExpandableListAdapter
to create a customized adapter tailored to specific data and view requirements.class CustomExpandableListAdapter : BaseExpandableListAdapter() { // Implement custom methods and override necessary functions }
Adding groups and children with BaseExpandableListAdapter:
getGroup
and getChild
to provide data for groups and children in the ExpandableListView.override fun getGroup(groupPosition: Int): Any { // Return group data based on position } override fun getChild(groupPosition: Int, childPosition: Int): Any { // Return child data based on group and child positions }
Handling click events in BaseExpandableListAdapter:
onGroupClick
and onChildClick
methods to handle click events for groups and children.override fun onGroupClick( parent: ExpandableListView?, v: View?, groupPosition: Int, id: Long ): Boolean { // Handle group click event return true } override fun onChildClick( parent: ExpandableListView?, v: View?, groupPosition: Int, childPosition: Int, id: Long ): Boolean { // Handle child click event return true }
Populating ExpandableListView with BaseExpandableListAdapter:
getGroupCount
and getChildrenCount
.override fun getGroupCount(): Int { // Return the total number of groups } override fun getChildrenCount(groupPosition: Int): Int { // Return the number of children in a specific group }
Implementing a collapsible list in Android with BaseExpandableListAdapter:
isChildSelectable
and isGroupSelectable
.override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean { // Return true if the child is selectable } override fun isGroupSelectable(groupPosition: Int): Boolean { // Return true if the group is selectable }
Updating data dynamically with BaseExpandableListAdapter:
notifyDataSetChanged
.fun updateData(newData: List<GroupData>) { // Update the dataset dataSet = newData // Notify the adapter about the changes notifyDataSetChanged() }