ExpandableListView的使用指南

ExpandableListView可以实现二级列表功能,可以展开,折叠,用例如下

adapter代码:

package com.example.expandablelistviewdemo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.TextView

/**
 * Created on 2021/4/26.
 */
/**
 * @param context
 * @param groups 每组标题
 * @param childs 每组子集合的集合
 */
class ExpandableListViewAdapter constructor(
    val context: Context,
    val groups: MutableList<String>,
    val childs: MutableList<MutableList<String>>
) : BaseExpandableListAdapter() {
    /**
     * 获取每组标题
     */
    override fun getGroup(groupPosition: Int): Any {
        return groups[groupPosition]
    }

    /**
     * 指定位置上的子元素是否可选中
     */
    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        if (childPosition == 2) {
            return false
        }
        return true
    }

    /**
     * 分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
     */
    override fun hasStableIds(): Boolean {
        return true
    }

    /**
     * 标题view
     */
    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val itemView =
            LayoutInflater.from(context).inflate(R.layout.item_group_adapter, parent, false)
        val tv_group_title = itemView.findViewById<TextView>(R.id.tv_group_title)

        tv_group_title.setText(groups[groupPosition])

        return itemView
    }

    /**
     * 每组子集合数量
     */
    override fun getChildrenCount(groupPosition: Int): Int {
        return childs[groupPosition].size
    }

    /**
     * 子item数据
     */
    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return childs[groupPosition][childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    /**
     * 子itemView
     */
    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        val itemView =
            LayoutInflater.from(context).inflate(R.layout.item_child_adapter, parent, false)
        val tv_child_title = itemView.findViewById<TextView>(R.id.tv_child_title)

        tv_child_title.setText(childs[groupPosition][childPosition])

        return itemView
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    /**
     * 共多少组
     */
    override fun getGroupCount(): Int {
        return groups.size
    }
}

其中标题view:

<?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="40dp">

    <TextView
        android:id="@+id/tv_group_title"
        android:gravity="center"
        android:text="content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

其中子itemView

<?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="40dp">

    <TextView
        android:id="@+id/tv_child_title"
        android:gravity="center"
        android:text="content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

最后listView设置adapter

package com.example.expandablelistviewdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    val TAG = "MainActivity"

    //每组的标题
    val groups = mutableListOf<String>()

    //每组子集合的集合
    val childs = mutableListOf<MutableList<String>>()
    val listView by lazy { findViewById<ExpandableListView>(R.id.lv_main) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        groups.add("广东")
        childs.add(mutableListOf("广州", "深圳", "汕头", "汕尾"))

        groups.add("广西")
        childs.add(mutableListOf("桂林", "广西1", "广西2", "广西3"))

        groups.add("湖北")
        childs.add(mutableListOf("武汉", "孝感", "仙桃", "天门"))

        groups.add("湖南")
        childs.add(mutableListOf("永州", "长沙", "常德", "岳阳"))

        val adapter = ExpandableListViewAdapter(this, groups, childs)
        listView.setAdapter(adapter)

        //默认展开第1组
        listView.expandGroup(1)

        //点击标题栏
        listView.setOnGroupClickListener { parent, v, groupPosition, id ->
            Log.e(TAG, "标题:${groups[groupPosition]}被点击了")
            return@setOnGroupClickListener false
        }

        //点击子item
        listView.setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
            Log.e(TAG, "子item:${childs[groupPosition][childPosition]}被点击了")
            return@setOnChildClickListener false
        }

        //组折叠通知
        listView.setOnGroupCollapseListener {
            Log.e(TAG, "标题:${groups[it]}折叠了")
        }

        //组展开通知
        listView.setOnGroupExpandListener {
            Log.e(TAG, "标题:${groups[it]}展开了")

        }


    }
}

其中布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

最后效果图:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容