这个东西用法基本固定,不知道能不能做三级的展开。
界面代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:childIndicator="@mipmap/ic_launcher" android:childIndicatorStart="380dp" android:childIndicatorEnd="410dp"/> </LinearLayout>
主程序代码
package com.example.expandablelistview import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.Gravity import android.view.View import android.view.ViewGroup import android.widget.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val adapter = object:BaseExpandableListAdapter() { internal var logos = intArrayOf(R.drawable.p, R.drawable.z, R.drawable.t) private val armTypes = arrayOf("神族兵种", "虫族兵种", "人族兵种") private val arms = arrayOf(arrayOf("狂战士", "龙骑士", "黑暗圣堂", "电兵"), arrayOf("小狗", "刺蛇", "飞龙", "自爆飞机"), arrayOf("机枪兵", "护士MM", "幽灵")) private val textView: TextView get() { val textView = TextView(this@MainActivity) val lp = AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) textView.layoutParams = lp textView.gravity = Gravity.CENTER_VERTICAL or Gravity.START textView.setPadding(36, 10, 0, 10) textView.textSize = 20f return textView } // 获取指定组位置、指定子列表项处的子列表项数据 override fun getChild(groupPosition: Int, childPosition: Int): Any { return arms[groupPosition][childPosition] } override fun getChildId(groupPosition: Int, childPosition: Int): Long { return childPosition.toLong() } override fun getChildrenCount(groupPosition: Int): Int { return arms[groupPosition].size } // 该方法决定每个子选项的外观 override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup): View { val textView: TextView if (convertView == null) { textView = this.textView textView.text = getChild(groupPosition, childPosition).toString() } else { textView = convertView as TextView } return textView } // 获取指定组位置处的组数据 override fun getGroup(groupPosition: Int): Any { return armTypes[groupPosition] } override fun getGroupCount(): Int { return armTypes.size } override fun getGroupId(groupPosition: Int): Long { return groupPosition.toLong() } // 该方法决定每个组选项的外观 override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View { val ll: LinearLayout if (convertView == null) { ll = LinearLayout(this@MainActivity) ll.orientation = LinearLayout.HORIZONTAL val logo = ImageView(this@MainActivity) logo.setImageResource(logos[groupPosition]) ll.addView(logo) val textView = this.textView textView.text = getGroup(groupPosition).toString() ll.addView(textView) } else { ll = convertView as LinearLayout } return ll } override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean { return true } override fun hasStableIds(): Boolean { return true } } val expandListView = findViewById<ExpandableListView>(R.id.list) expandListView.setAdapter(adapter) } }