@@ -45,7 +45,7 @@ fun <T> ViewPager2.bind(items: List<T>): FastListAdapter<T> {
45
45
fun <T > RecyclerView.bind (items : List <T >, @LayoutRes singleLayout : Int = 0, singleBind : BindingClosure <T >): FastListAdapter <T > {
46
46
layoutManager = LinearLayoutManager (context)
47
47
return FastListAdapter (items.toMutableList(), this
48
- ).map(singleLayout, {item: T , idx : Int -> true }, singleBind)
48
+ ).map(singleLayout, { item: T , position : Int -> true }, singleBind)
49
49
}
50
50
51
51
/* *
@@ -58,10 +58,9 @@ fun <T> RecyclerView.bind(items: List<T>, @LayoutRes singleLayout: Int = 0, sing
58
58
*/
59
59
fun <T > ViewPager2.bind (items : List <T >, @LayoutRes singleLayout : Int = 0, singleBind : BindingClosure <T >): FastListAdapter <T > {
60
60
return FastListAdapter (items.toMutableList(), vpList = this
61
- ).map(singleLayout, {item: T , idx : Int -> true }, singleBind)
61
+ ).map(singleLayout, { item: T , position : Int -> true }, singleBind)
62
62
}
63
63
64
-
65
64
/* *
66
65
* Updates the list using DiffUtils.
67
66
* @param newItems the new list which is to replace the old one.
@@ -85,7 +84,7 @@ fun <T> ViewPager2.update(newItems: List<T>) {
85
84
(adapter as ? FastListAdapter <T >)?.update(newItems) { o, n, _ -> o == n }
86
85
}
87
86
88
- open class FastListAdapter <T >(private var items : MutableList <T >, private var list : RecyclerView ? = null , private var vpList : ViewPager2 ? = null )
87
+ open class FastListAdapter <T >(private var items : MutableList <T >, private var list : RecyclerView ? = null , private var vpList : ViewPager2 ? = null )
89
88
: RecyclerView .Adapter <FastListViewHolder <T >>() {
90
89
91
90
init {
@@ -96,11 +95,12 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
96
95
97
96
}
98
97
99
- private inner class BindMap (val layout : Int , var type : Int = 0 , val bind : BindingClosure <T >, val predicate : (item: T , idx : Int ) -> Boolean ) {
100
- constructor (lf: LayoutFactory , type: Int = 0 , bind: BindingClosure <T >, predicate: (item: T , idx : Int ) -> Boolean ) : this (0 , type, bind, predicate){
98
+ private inner class BindMap (val layout : Int , var type : Int = 0 , val bind : BindingClosure <T >, val predicate : (item: T , position : Int ) -> Boolean ) {
99
+ constructor (lf: LayoutFactory , type: Int = 0 , bind: BindingClosure <T >, predicate: (item: T , position : Int ) -> Boolean ) : this (0 , type, bind, predicate) {
101
100
layoutFactory = lf
102
101
}
103
- var layoutFactory : LayoutFactory ? = null
102
+
103
+ var layoutFactory: LayoutFactory ? = null
104
104
}
105
105
106
106
private var bindMap = mutableListOf<BindMap >()
@@ -110,7 +110,7 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
110
110
return bindMap.first { it.type == viewType }.let {
111
111
it.layoutFactory?.let {
112
112
return FastListViewHolder (it.createView(parent, viewType), viewType)
113
- } ? : run {
113
+ } ? : run {
114
114
return FastListViewHolder (LayoutInflater .from(parent.context).inflate(it.layout,
115
115
parent, false ), viewType)
116
116
}
@@ -137,22 +137,21 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
137
137
* @param bind - The "binding" function between the item and the layout. This is the standard "bind" function in traditional ViewHolder classes. It uses Kotlin Extensions
138
138
* so you can just use the XML names of the views inside your layout to address them.
139
139
*/
140
- fun map (@LayoutRes layout : Int , predicate : (item: T , idx : Int ) -> Boolean , bind : BindingClosure <T >): FastListAdapter <T > {
140
+ fun map (@LayoutRes layout : Int , predicate : (item: T , position : Int ) -> Boolean , bind : BindingClosure <T >): FastListAdapter <T > {
141
141
bindMap.add(BindMap (layout, typeCounter++ , bind, predicate))
142
142
list?.adapter = this
143
143
vpList?.adapter = this
144
144
return this
145
145
}
146
146
147
-
148
147
/* *
149
148
* The function used for mapping types to layouts
150
149
* @param layoutFactory - factory that creates the view for this adapter
151
150
* @param predicate - Function used to sort the items. For example, a Type field inside your items class with different values for different types.
152
151
* @param bind - The "binding" function between the item and the layout. This is the standard "bind" function in traditional ViewHolder classes. It uses Kotlin Extensions
153
152
* so you can just use the XML names of the views inside your layout to address them.
154
153
*/
155
- fun map (layoutFactory : LayoutFactory , predicate : (item: T , idx : Int ) -> Boolean , bind : BindingClosure <T >): FastListAdapter <T > {
154
+ fun map (layoutFactory : LayoutFactory , predicate : (item: T , position : Int ) -> Boolean , bind : BindingClosure <T >): FastListAdapter <T > {
156
155
bindMap.add(BindMap (layoutFactory, typeCounter++ , bind, predicate))
157
156
list?.adapter = this
158
157
vpList?.adapter = this
@@ -163,7 +162,7 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
163
162
* Sets up a layout manager for the recycler view.
164
163
*/
165
164
fun layoutManager (manager : RecyclerView .LayoutManager ): FastListAdapter <T > {
166
- vpList?.let { throw UnsupportedOperationException (" layoumanager not needed for ViewPager2" )}
165
+ vpList?.let { throw UnsupportedOperationException (" layoumanager not needed for ViewPager2" ) }
167
166
list!! .layoutManager = manager
168
167
return this
169
168
}
@@ -193,7 +192,7 @@ open class FastListAdapter<T>(private var items: MutableList<T>, private var lis
193
192
}
194
193
195
194
interface LayoutFactory {
196
- fun createView (parent : ViewGroup , type : Int ) : View
195
+ fun createView (parent : ViewGroup , type : Int ): View
197
196
}
198
197
199
198
class FastListViewHolder <T >(override val containerView : View , val holderType : Int ) : RecyclerView.ViewHolder(containerView), LayoutContainer {
0 commit comments