1+ package com.yhw.library.adapter
2+
3+ import android.text.TextUtils
4+ import android.view.LayoutInflater
5+ import android.view.View
6+ import android.view.ViewGroup
7+ import android.widget.BaseExpandableListAdapter
8+ import android.widget.CheckBox
9+ import android.widget.ImageView
10+ import android.widget.TextView
11+ import androidx.annotation.DrawableRes
12+ import androidx.annotation.IdRes
13+ import androidx.annotation.StringRes
14+ import com.yhw.library.data.ExpandableData
15+
16+ abstract class BaseSimpleExpandableListAdapter <G , C >(private var dataList : MutableList <ExpandableData <G , C >>) :
17+ BaseExpandableListAdapter () {
18+ override fun getGroupCount (): Int {
19+ return dataList.size
20+ }
21+
22+ override fun getChildrenCount (groupPosition : Int ): Int {
23+ return dataList[groupPosition].childList.size
24+ }
25+
26+ override fun getGroup (groupPosition : Int ): G {
27+ return dataList[groupPosition].group
28+ }
29+
30+ override fun getChild (groupPosition : Int , childPosition : Int ): C {
31+ return dataList[groupPosition].childList[childPosition]
32+ }
33+
34+ override fun getGroupId (groupPosition : Int ): Long {
35+ return groupPosition.toLong()
36+ }
37+
38+ override fun getChildId (groupPosition : Int , childPosition : Int ): Long {
39+ return childPosition.toLong()
40+ }
41+
42+ override fun hasStableIds (): Boolean {
43+ return false
44+ }
45+
46+ override fun isChildSelectable (groupPosition : Int , childPosition : Int ): Boolean {
47+ return true
48+ }
49+
50+
51+ override fun getGroupView (
52+ groupPosition : Int ,
53+ isExpanded : Boolean ,
54+ convertView : View ? ,
55+ parent : ViewGroup ?
56+ ): View {
57+ val viewHolder: ViewHolder
58+ val view: View
59+ if (convertView == null ) {
60+ view = LayoutInflater .from(parent?.context)
61+ .inflate(getGroupItemLayoutId(), parent, false )!!
62+ viewHolder = ViewHolder (view)
63+ viewHolder.itemViewType = getGroupType(groupPosition)
64+ view.tag = viewHolder
65+ } else {
66+ view = convertView
67+ viewHolder = view.tag as ViewHolder
68+ }
69+ onBindGroupViewItem(
70+ groupPosition,
71+ dataList[groupPosition].group,
72+ parent,
73+ viewHolder,
74+ isExpanded
75+ )
76+ return view
77+ }
78+
79+ override fun getChildView (
80+ groupPosition : Int ,
81+ childPosition : Int ,
82+ isLastChild : Boolean ,
83+ convertView : View ? ,
84+ parent : ViewGroup ?
85+ ): View {
86+ val viewHolder: ViewHolder
87+ val view: View
88+ if (convertView == null ) {
89+ view = LayoutInflater .from(parent?.context)
90+ .inflate(getChildItemLayoutId(), parent, false )!!
91+ viewHolder = ViewHolder (view)
92+ viewHolder.itemViewType = getChildType(groupPosition, childPosition)
93+ view.tag = viewHolder
94+ } else {
95+ view = convertView
96+ viewHolder = view.tag as ViewHolder
97+ }
98+ onBindChildViewItem(
99+ groupPosition,
100+ childPosition,
101+ dataList[groupPosition].childList[childPosition],
102+ parent,
103+ viewHolder,
104+ isLastChild
105+ )
106+ return view
107+ }
108+
109+ class ViewHolder (var itemView : View ) {
110+ private var viewMap: MutableMap <Int , View > = mutableMapOf ()
111+ var itemViewType = 0
112+
113+ fun <T : View > getView (@IdRes viewId : Int ): T {
114+ var view = viewMap[viewId]
115+ if (view == null ) {
116+ view = itemView.findViewById(viewId)
117+ viewMap[viewId] = view
118+ }
119+ return view as T
120+ }
121+
122+ fun setText (@IdRes viewId : Int , text : String ) {
123+ val textView: TextView = getView(viewId)
124+ textView.text = text
125+ }
126+
127+ fun setText (@IdRes viewId : Int , @StringRes textId : Int ) {
128+ val textView: TextView = getView(viewId)
129+ textView.setText(textId)
130+ }
131+
132+ /* *
133+ * 设置文本
134+ * 如果文本为null或者空字符串则隐藏控件
135+ */
136+ fun setTextIfEmptyHide (@IdRes viewId : Int , text : String ) {
137+ val textView: TextView = getView(viewId)
138+ if (TextUtils .isEmpty(text)) {
139+ textView.visibility = View .GONE
140+ } else {
141+ textView.visibility = View .VISIBLE
142+ textView.text = text
143+ }
144+ }
145+
146+ fun setImageResource (@IdRes viewId : Int , @DrawableRes resId : Int ) {
147+ val imageView: ImageView = getView(viewId)
148+ imageView.setImageResource(resId)
149+ }
150+
151+ fun setChecked (@IdRes viewId : Int , isChecked : Boolean ) {
152+ val checkBox: CheckBox = getView(viewId)
153+ checkBox.isChecked = isChecked
154+ }
155+ }
156+
157+ abstract fun getGroupItemLayoutId (): Int
158+ abstract fun getChildItemLayoutId (): Int
159+
160+ /* *
161+ * 分组数据绑定
162+ * @param groupPosition 索引位置
163+ * @param groupData 分组数据
164+ * @param holder 获取控件
165+ */
166+ abstract fun onBindGroupViewItem (
167+ groupPosition : Int ,
168+ groupData : G ,
169+ parent : ViewGroup ? ,
170+ holder : ViewHolder ,
171+ isExpanded : Boolean
172+ )
173+
174+ /* *
175+ * 子数据绑定
176+ * @param groupPosition 分组索引位置
177+ * @param childPosition 子数据索引位置
178+ * @param childData 子数据
179+ * @param holder 获取控件
180+ */
181+ abstract fun onBindChildViewItem (
182+ groupPosition : Int ,
183+ childPosition : Int ,
184+ childData : C ,
185+ parent : ViewGroup ? ,
186+ holder : ViewHolder ,
187+ isLastChild : Boolean
188+ )
189+
190+ fun removeGroupAt (groupPosition : Int ) {
191+ this .dataList.removeAt(groupPosition)
192+ this .notifyDataSetChanged()
193+ }
194+
195+ fun removeChildAt (groupPosition : Int , childPosition : Int ) {
196+ this .dataList[groupPosition].childList.removeAt(childPosition)
197+ this .notifyDataSetChanged()
198+ }
199+
200+ fun removeAll () {
201+ this .dataList.clear()
202+ this .notifyDataSetChanged()
203+ }
204+
205+ fun refreshGroupItem (group : G , groupPosition : Int ) {
206+ this .dataList[groupPosition].group = group
207+ this .notifyDataSetChanged()
208+ }
209+
210+ fun refreshChildItem (child : C , groupPosition : Int , childPosition : Int ) {
211+ this .dataList[groupPosition].childList[childPosition] = child
212+ this .notifyDataSetChanged()
213+ }
214+
215+ fun refreshAll (dataList : MutableList <ExpandableData <G , C >>) {
216+ this .dataList = dataList
217+ this .notifyDataSetChanged()
218+ }
219+ }
0 commit comments