ExpandableListView的使用指南

ExpandableListView可以實現(xiàn)二級列表功能,可以展開,折疊,用例如下

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
    }

    /**
     * 分組和子選項是否持有穩(wěn)定的ID, 就是說底層數(shù)據(jù)的改變會不會影響到它們
     */
    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
    }

    /**
     * 每組子集合數(shù)量
     */
    override fun getChildrenCount(groupPosition: Int): Int {
        return childs[groupPosition].size
    }

    /**
     * 子item數(shù)據(jù)
     */
    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設(shè)置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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容