@@ -16,14 +16,11 @@ import java.util.*
1616 * create by xiaoxiaoying on 2019-06-21
1717 * @author xiaoxiaoying
1818 */
19- abstract class ArrayAdapter <T , H : ArrayAdapter .ViewHolder <T >>(
19+ abstract class ArrayAdapter <T , H : ArrayAdapter .ViewHolder <T >> @JvmOverloads constructor (
2020 private val context : Context ,
2121 @LayoutRes private val resId : Int = 0 ,
22- private val arrays : MutableList <T >
22+ private val arrays : MutableList <T > = mutableListOf()
2323) : RecyclerView.Adapter<H>() {
24- constructor (context: Context ) : this (context, 0 )
25- constructor (context: Context , resource: Int ) : this (context, resource, ArrayList <T >())
26-
2724
2825 companion object {
2926 private val mLock = Any ()
@@ -73,6 +70,26 @@ abstract class ArrayAdapter<T, H : ArrayAdapter.ViewHolder<T>>(
7370 notifyItemRangeInserted(index, 1 )
7471 }
7572
73+ fun insert (vararg objects : T , startIndex : Int ) {
74+ if (startIndex > arrays.size) return
75+ synchronized(mLock)
76+ {
77+ objects.forEachIndexed { index, t ->
78+ insert(t, index + startIndex)
79+ }
80+ }
81+ }
82+
83+ fun insert (collection : Collection <T >, startIndex : Int ) {
84+ if (startIndex > arrays.size) return
85+ synchronized(mLock)
86+ {
87+ collection.forEachIndexed { index, t ->
88+ insert(t, index + startIndex)
89+ }
90+ }
91+ }
92+
7693 /* *
7794 * Removes the specified object from the array.
7895 *
@@ -114,12 +131,14 @@ abstract class ArrayAdapter<T, H : ArrayAdapter.ViewHolder<T>>(
114131 fun removeItem (count : Int , startPosition : Int ) {
115132 synchronized(mLock)
116133 {
117- for (position in 0 until count) {
118- val deletePosition = position + startPosition
119- if (arrays.size <= deletePosition) return
120- arrays.removeAt(deletePosition)
134+ repeat(count) {
135+ val deletePosition = it - 1 + startPosition
136+ if (arrays.size > deletePosition) {
137+ arrays.removeAt(deletePosition)
138+ }
121139
122140 }
141+
123142 notifyItemRangeRemoved(startPosition, count)
124143 }
125144 }
@@ -132,6 +151,10 @@ abstract class ArrayAdapter<T, H : ArrayAdapter.ViewHolder<T>>(
132151 */
133152 fun findItemPosition (item : T ): Int = arrays.indexOf(item)
134153
154+ fun find (predicate : (T ) -> Boolean ) {
155+ arrays.find(predicate)
156+ }
157+
135158 fun clean () {
136159 synchronized(mLock)
137160 {
0 commit comments