Skip to content

Commit fac698a

Browse files
committed
新增ExpandableListView和ViewPager2+Fragment的适配器
1 parent ffe1e2f commit fac698a

18 files changed

+651
-16
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.yhw.library.adapter
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentActivity
5+
import androidx.viewpager2.adapter.FragmentStateAdapter
6+
7+
class BaseViewPager2FragmentAdapter(
8+
fragmentActivity: FragmentActivity,
9+
private var fragmentList: MutableList<Fragment>
10+
) : FragmentStateAdapter(fragmentActivity), IAdapter<Fragment> {
11+
override fun getItemCount(): Int {
12+
return fragmentList.size
13+
}
14+
15+
override fun createFragment(position: Int): Fragment {
16+
return fragmentList[position]
17+
}
18+
19+
override fun refreshAll(dataList: MutableList<Fragment>) {
20+
this.fragmentList = dataList
21+
this.notifyDataSetChanged()
22+
}
23+
24+
override fun refreshItem(data: Fragment, position: Int) {
25+
if (position >= itemCount) {
26+
return
27+
}
28+
this.fragmentList[position] = data
29+
this.notifyItemChanged(position)
30+
}
31+
32+
/**
33+
* 插入数据
34+
*/
35+
override fun insertItem(data: Fragment, position: Int) {
36+
if (position > itemCount) {
37+
return
38+
}
39+
this.fragmentList.add(position, data)
40+
this.notifyItemRangeInserted(position, 1)
41+
if (position + 1 < itemCount) {
42+
this.notifyItemRangeChanged(position + 1, itemCount - position - 1)
43+
}
44+
}
45+
46+
/**
47+
* 首部插入数据
48+
*/
49+
override fun insertItemToFirst(data: Fragment) {
50+
this.fragmentList.add(0, data)
51+
this.notifyItemInserted(0)
52+
if (itemCount > 1) {
53+
this.notifyItemRangeChanged(1, itemCount - 1)
54+
}
55+
}
56+
57+
/**
58+
* 首部插入多条数据
59+
*/
60+
override fun insertItemToFirst(dataList: MutableList<Fragment>) {
61+
this.fragmentList.addAll(0, dataList)
62+
this.notifyItemRangeInserted(0, dataList.size)
63+
if (itemCount > dataList.size) {
64+
this.notifyItemRangeChanged(dataList.size, itemCount - dataList.size)
65+
}
66+
}
67+
68+
/**
69+
* 尾部追加数据
70+
*/
71+
override fun appendItem(data: Fragment) {
72+
this.fragmentList.add(data)
73+
this.notifyItemInserted(itemCount)
74+
}
75+
76+
/**
77+
* 尾部追加多条数据
78+
*/
79+
override fun appendItem(dataList: MutableList<Fragment>) {
80+
this.fragmentList.addAll(dataList)
81+
this.notifyItemRangeInserted(itemCount, dataList.size)
82+
}
83+
84+
override fun removeAt(position: Int) {
85+
if (position >= itemCount) {
86+
return
87+
}
88+
this.fragmentList.removeAt(position)
89+
this.notifyItemRemoved(position)
90+
if (itemCount > 0 && position < itemCount) {
91+
this.notifyItemRangeChanged(position, itemCount - position)
92+
}
93+
}
94+
95+
override fun removeAt(positionList: MutableList<Int>) {
96+
if (positionList.size >= itemCount) {
97+
return
98+
}
99+
for (i in positionList) {
100+
this.fragmentList.removeAt(i)
101+
}
102+
this.notifyDataSetChanged()
103+
}
104+
105+
/**
106+
* 清除所有数据
107+
*/
108+
override fun removeAll() {
109+
this.fragmentList.clear()
110+
this.notifyDataSetChanged()
111+
}
112+
113+
}

library/src/main/java/com/yhw/library/adapter/BaseViewPagerFragmentAdapter.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@ package com.yhw.library.adapter
22

33
import androidx.fragment.app.Fragment
44
import androidx.fragment.app.FragmentManager
5-
import androidx.fragment.app.FragmentPagerAdapter
5+
import androidx.fragment.app.FragmentStatePagerAdapter
66

77
/**
88
* ViewPage的基类adapter
99
* Fragment
1010
*/
11-
class BaseViewPagerFragmentAdapter(
12-
fm: FragmentManager,
11+
class BaseViewPagerFragmentAdapter : FragmentStatePagerAdapter, IAdapter<Fragment> {
1312
private var fragmentList: MutableList<Fragment>
14-
) : FragmentPagerAdapter(fm), IAdapter<Fragment> {
13+
14+
constructor(
15+
fm: FragmentManager,
16+
fragmentList: MutableList<Fragment>
17+
) : super(fm) {
18+
this.fragmentList = fragmentList
19+
}
20+
21+
/**
22+
* @param behavior FragmentStatePagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT,
23+
* FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
24+
*/
25+
constructor(
26+
fm: FragmentManager,
27+
behavior: Int,
28+
fragmentList: MutableList<Fragment>
29+
) : super(fm, behavior) {
30+
this.fragmentList = fragmentList
31+
}
32+
33+
1534
override fun getCount(): Int {
1635
return fragmentList.size
1736
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.yhw.library.data
2+
3+
/**
4+
* BaseExpandableListAdapter的数据类
5+
* @param G 分组类型
6+
* @param C 子数据类型
7+
*/
8+
data class ExpandableData<G, C>(
9+
var group: G,
10+
var childList: MutableList<C>
11+
)

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies {
4040
implementation 'com.google.android.material:material:1.1.0'
4141
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
4242
implementation project(path: ':library')
43-
// implementation 'com.yhw.library:fastadapter:0.0.1'
43+
// implementation 'com.yhw.library:fastadapter:1.0.0'
4444
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
4545
testImplementation 'junit:junit:4.+'
4646
androidTestImplementation 'androidx.test.ext:junit:1.1.1'

0 commit comments

Comments
 (0)