ExpandableListAdapter なのですから、 ExpandableListView で使うのではないでしょうか。
なぜ ExpandableListAdapter に辿り着いて ExpandableListView に辿り着かないのかが不思議な感じがしますが。
公式ドキュメントに真っ先に
An adapter that links a ExpandableListView with the underlying data.
と書いてあります。
MainActivity.kt
kotlin
1import androidx.appcompat.app.AppCompatActivity
2import android.os.Bundle
3import android.view.LayoutInflater
4import android.view.View
5import android.view.ViewGroup
6import android.widget.BaseExpandableListAdapter
7import android.widget.ExpandableListView
8
9class MainActivity : AppCompatActivity() {
10 override fun onCreate(savedInstanceState: Bundle?) {
11 super.onCreate(savedInstanceState)
12 setContentView(R.layout.activity_main)
13
14 var exList = findViewById<ExpandableListView>(R.id.exList)
15 exList.setAdapter(ExListAdapter())
16 }
17}
18
19class ExListAdapter : BaseExpandableListAdapter() {
20 override fun getGroupCount(): Int = 1
21 override fun getGroupId(groupPosition: Int): Long = 0
22 override fun getGroup(groupPosition: Int): Any = ""
23 override fun getGroupView(groupPosition: Int,
24 isExpanded: Boolean,
25 convertView: View?,
26 parent: ViewGroup): View {
27 var view = convertView
28 if(view == null) {
29 view = LayoutInflater.from(parent.context).inflate(R.layout.test1, parent, false)
30 }
31 return view!!
32 }
33
34 override fun getChildrenCount(groupPosition: Int): Int = 1
35 override fun getChild(groupPosition: Int, childPosition: Int): Any = ""
36 override fun getChildId(groupPosition: Int, childPosition: Int): Long = 0
37 override fun getChildView(groupPosition: Int,
38 childPosition: Int,
39 isLastChild: Boolean,
40 convertView: View?,
41 parent: ViewGroup): View {
42 var view = convertView
43 if(view == null) {
44 view = LayoutInflater.from(parent.context).inflate(R.layout.test2, parent, false)
45 }
46 return view!!
47 }
48 override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = false
49
50 override fun hasStableIds(): Boolean = true
51}
res/layout/activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<ExpandableListView
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 tools:context=".MainActivity"
6 android:id="@+id/exList"
7 android:layout_width="match_parent"
8 android:layout_height="match_parent" />
res/layout/test1.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<TextView
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:text="Text 1"
7 android:paddingStart="30dp"/>
res/layout/test2.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<TextView
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:text="Text 2"
7 android:paddingStart="35dp"/>

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/30 14:53