Skip to content

Commit bf3bdae

Browse files
perf(*): 优化NormalRecyclerAdapter、SingleChoiceAdapter、MultipleChoiceAdapter
1 parent e2fa071 commit bf3bdae

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

lite/src/main/java/com/pengxh/kt/lite/adapter/MultipleChoiceAdapter.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pengxh.kt.lite.adapter
22

3+
import android.annotation.SuppressLint
34
import android.view.LayoutInflater
45
import android.view.ViewGroup
56
import androidx.annotation.LayoutRes
@@ -9,7 +10,7 @@ import androidx.recyclerview.widget.RecyclerView
910
* RecyclerView多选适配器
1011
*/
1112
abstract class MultipleChoiceAdapter<T>(
12-
@LayoutRes private val xmlResource: Int, private val dataRows: List<T>
13+
@LayoutRes private val xmlResource: Int, private val dataRows: MutableList<T>
1314
) : RecyclerView.Adapter<ViewHolder>() {
1415

1516
private val kTag = "MultipleChoiceAdapter"
@@ -41,6 +42,18 @@ abstract class MultipleChoiceAdapter<T>(
4142
}
4243
}
4344

45+
@SuppressLint("NotifyDataSetChanged")
46+
fun setRefreshData(dataRows: MutableList<T>) {
47+
this.dataRows.clear()
48+
this.dataRows.addAll(dataRows)
49+
notifyDataSetChanged()
50+
}
51+
52+
fun setLoadMoreData(dataRows: MutableList<T>) {
53+
this.dataRows.addAll(dataRows)
54+
notifyItemRangeInserted(this.dataRows.size, dataRows.size)
55+
}
56+
4457
abstract fun convertView(viewHolder: ViewHolder, position: Int, item: T)
4558

4659
private var itemCheckedListener: OnItemCheckedListener<T>? = null

lite/src/main/java/com/pengxh/kt/lite/adapter/NormalRecyclerAdapter.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pengxh.kt.lite.adapter
22

3+
import android.annotation.SuppressLint
34
import android.view.LayoutInflater
45
import android.view.ViewGroup
56
import androidx.annotation.LayoutRes
@@ -10,7 +11,7 @@ import androidx.recyclerview.widget.RecyclerView
1011
* RecyclerView普通列表适配器
1112
*/
1213
abstract class NormalRecyclerAdapter<T>(
13-
@LayoutRes private val xmlResource: Int, private val dataRows: List<T>
14+
@LayoutRes private val xmlResource: Int, private val dataRows: MutableList<T>
1415
) : RecyclerView.Adapter<ViewHolder>() {
1516

1617
private val kTag = "NormalRecyclerAdapter"
@@ -28,6 +29,18 @@ abstract class NormalRecyclerAdapter<T>(
2829
}
2930
}
3031

32+
@SuppressLint("NotifyDataSetChanged")
33+
fun setRefreshData(dataRows: MutableList<T>) {
34+
this.dataRows.clear()
35+
this.dataRows.addAll(dataRows)
36+
notifyDataSetChanged()
37+
}
38+
39+
fun setLoadMoreData(dataRows: MutableList<T>) {
40+
this.dataRows.addAll(dataRows)
41+
notifyItemRangeInserted(this.dataRows.size, dataRows.size)
42+
}
43+
3144
abstract fun convertView(viewHolder: ViewHolder, position: Int, item: T)
3245

3346
private var itemClickedListener: OnItemClickedListener<T>? = null

lite/src/main/java/com/pengxh/kt/lite/adapter/SingleChoiceAdapter.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pengxh.kt.lite.adapter
22

3+
import android.annotation.SuppressLint
34
import android.view.LayoutInflater
45
import android.view.ViewGroup
56
import androidx.annotation.LayoutRes
@@ -10,7 +11,7 @@ import androidx.recyclerview.widget.RecyclerView
1011
* RecyclerView单选适配器
1112
*/
1213
abstract class SingleChoiceAdapter<T>(
13-
@LayoutRes private val xmlResource: Int, private val dataRows: List<T>
14+
@LayoutRes private val xmlResource: Int, private val dataRows: MutableList<T>
1415
) : RecyclerView.Adapter<ViewHolder>() {
1516

1617
private val kTag = "SingleChoiceAdapter"
@@ -43,6 +44,18 @@ abstract class SingleChoiceAdapter<T>(
4344
}
4445
}
4546

47+
@SuppressLint("NotifyDataSetChanged")
48+
fun setRefreshData(dataRows: MutableList<T>) {
49+
this.dataRows.clear()
50+
this.dataRows.addAll(dataRows)
51+
notifyDataSetChanged()
52+
}
53+
54+
fun setLoadMoreData(dataRows: MutableList<T>) {
55+
this.dataRows.addAll(dataRows)
56+
notifyItemRangeInserted(this.dataRows.size, dataRows.size)
57+
}
58+
4659
abstract fun convertView(viewHolder: ViewHolder, position: Int, item: T)
4760

4861
private var itemCheckedListener: OnItemCheckedListener<T>? = null

0 commit comments

Comments
 (0)