优化
This commit is contained in:
@@ -8,6 +8,8 @@ import androidx.recyclerview.widget.DiffUtil
|
|||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import splitties.views.onLongClick
|
import splitties.views.onLongClick
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,57 +48,72 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
|
|||||||
recyclerView.adapter = this
|
recyclerView.adapter = this
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
suspend fun setItems(items: List<ITEM>?) {
|
||||||
fun setItems(items: List<ITEM>?) {
|
withContext(Dispatchers.Default) {
|
||||||
kotlin.runCatching {
|
synchronized(asyncListDiffer) {
|
||||||
if (items == null) {
|
kotlin.runCatching {
|
||||||
asyncListDiffer.submitList(null)
|
if (items == null) {
|
||||||
} else {
|
asyncListDiffer.submitList(null)
|
||||||
asyncListDiffer.submitList(ArrayList(items))
|
} else {
|
||||||
|
asyncListDiffer.submitList(ArrayList(items))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
suspend fun setItem(position: Int, item: ITEM) {
|
||||||
fun setItem(position: Int, item: ITEM) {
|
withContext(Dispatchers.Default) {
|
||||||
kotlin.runCatching {
|
synchronized(asyncListDiffer) {
|
||||||
val list = ArrayList(asyncListDiffer.currentList)
|
kotlin.runCatching {
|
||||||
list[position] = item
|
val list = ArrayList(asyncListDiffer.currentList)
|
||||||
asyncListDiffer.submitList(list)
|
list[position] = item
|
||||||
}
|
asyncListDiffer.submitList(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
fun updateItem(item: ITEM) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
val index = asyncListDiffer.currentList.indexOf(item)
|
|
||||||
if (index >= 0) {
|
|
||||||
asyncListDiffer.currentList[index] = item
|
|
||||||
notifyItemChanged(index)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
suspend fun updateItem(item: ITEM) {
|
||||||
fun updateItem(position: Int, payload: Any) {
|
withContext(Dispatchers.Main) {
|
||||||
kotlin.runCatching {
|
synchronized(asyncListDiffer) {
|
||||||
val size = itemCount
|
kotlin.runCatching {
|
||||||
if (position in 0 until size) {
|
val index = asyncListDiffer.currentList.indexOf(item)
|
||||||
notifyItemChanged(position, payload)
|
if (index >= 0) {
|
||||||
|
asyncListDiffer.currentList[index] = item
|
||||||
|
notifyItemChanged(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
suspend fun updateItem(position: Int, payload: Any) {
|
||||||
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
|
withContext(Dispatchers.Main) {
|
||||||
kotlin.runCatching {
|
synchronized(asyncListDiffer) {
|
||||||
val size = itemCount
|
kotlin.runCatching {
|
||||||
if (fromPosition in 0 until size && toPosition in 0 until size) {
|
val size = itemCount
|
||||||
notifyItemRangeChanged(
|
if (position in 0 until size) {
|
||||||
fromPosition,
|
notifyItemChanged(position, payload)
|
||||||
toPosition - fromPosition + 1,
|
}
|
||||||
payloads
|
}
|
||||||
)
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
synchronized(asyncListDiffer) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
val size = itemCount
|
||||||
|
if (fromPosition in 0 until size && toPosition in 0 until size) {
|
||||||
|
notifyItemRangeChanged(
|
||||||
|
fromPosition,
|
||||||
|
toPosition - fromPosition + 1,
|
||||||
|
payloads
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user