|
|
@@ -5,6 +5,7 @@ import android.content.Context
|
|
|
import android.util.SparseArray
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.ViewGroup
|
|
|
+import androidx.core.util.size
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
@@ -37,6 +38,8 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
|
|
|
private var diffJob: Coroutine<*>? = null
|
|
|
|
|
|
+ private var isResumed = false
|
|
|
+
|
|
|
var itemAnimation: ItemAnimation? = null
|
|
|
|
|
|
fun setOnItemClickListener(listener: (holder: ItemViewHolder, item: ITEM) -> Unit) {
|
|
|
@@ -54,8 +57,8 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
@Synchronized
|
|
|
fun addHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) {
|
|
|
kotlin.runCatching {
|
|
|
- val index = headerItems.size()
|
|
|
- headerItems.put(TYPE_HEADER_VIEW + headerItems.size(), header)
|
|
|
+ val index = headerItems.size
|
|
|
+ headerItems.put(TYPE_HEADER_VIEW + headerItems.size, header)
|
|
|
notifyItemInserted(index)
|
|
|
}
|
|
|
}
|
|
|
@@ -63,8 +66,8 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
@Synchronized
|
|
|
fun addFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) {
|
|
|
kotlin.runCatching {
|
|
|
- val index = getActualItemCount() + footerItems.size()
|
|
|
- footerItems.put(TYPE_FOOTER_VIEW + footerItems.size(), footer)
|
|
|
+ val index = getActualItemCount() + footerItems.size
|
|
|
+ footerItems.put(TYPE_FOOTER_VIEW + footerItems.size, footer)
|
|
|
notifyItemInserted(index)
|
|
|
}
|
|
|
}
|
|
|
@@ -113,6 +116,10 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
skipDiff: Boolean = false
|
|
|
) {
|
|
|
kotlin.runCatching {
|
|
|
+ if (!isResumed) { //全量标记更新
|
|
|
+ setItems(items)
|
|
|
+ return@runCatching
|
|
|
+ }
|
|
|
val oldItems = this.items.toList()
|
|
|
val itemsSize = items?.size ?: 0
|
|
|
val headerCount = getHeaderCount()
|
|
|
@@ -153,6 +160,10 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
return itemCallback.getChangePayload(oldItem, newItem)
|
|
|
}
|
|
|
}
|
|
|
+ if (!isResumed) {
|
|
|
+ setItems(items)
|
|
|
+ return@runCatching
|
|
|
+ }
|
|
|
diffJob?.cancel()
|
|
|
diffJob = Coroutine.async {
|
|
|
val diffResult = if (skipDiff) withTimeoutOrNullAsync(500L) {
|
|
|
@@ -162,7 +173,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
}
|
|
|
ensureActive()
|
|
|
handler.post {
|
|
|
- if (diffResult == null) {
|
|
|
+ if (isResumed || diffResult == null) {
|
|
|
setItems(items)
|
|
|
return@post
|
|
|
}
|
|
|
@@ -172,6 +183,10 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
if (items != null) {
|
|
|
this@RecyclerAdapter.items.addAll(items)
|
|
|
}
|
|
|
+ if (!isResumed) {
|
|
|
+ return@post
|
|
|
+ }
|
|
|
+ ensureActive()
|
|
|
diffResult.dispatchUpdatesTo(this@RecyclerAdapter)
|
|
|
onCurrentListChanged()
|
|
|
}
|
|
|
@@ -329,10 +344,10 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
fun getActualItemCount() = items.size
|
|
|
|
|
|
|
|
|
- fun getHeaderCount() = headerItems.size()
|
|
|
+ fun getHeaderCount() = headerItems.size
|
|
|
|
|
|
|
|
|
- fun getFooterCount() = footerItems.size()
|
|
|
+ fun getFooterCount() = footerItems.size
|
|
|
|
|
|
fun getItem(position: Int): ITEM? = items.getOrNull(position)
|
|
|
|
|
|
@@ -431,6 +446,15 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun upResumed(isResumed: Boolean) {
|
|
|
+ if (!isResumed) {
|
|
|
+ diffJob?.cancel()
|
|
|
+ diffJob = null
|
|
|
+ handler.removeCallbacksAndMessages(null)
|
|
|
+ }
|
|
|
+ this.isResumed = isResumed
|
|
|
+ }
|
|
|
+
|
|
|
private fun isHeader(position: Int) = position < getHeaderCount()
|
|
|
|
|
|
private fun isFooter(position: Int) = position >= getActualItemCount() + getHeaderCount()
|
|
|
@@ -479,8 +503,4 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|
|
private val handler by lazy { buildMainHandler() }
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+}
|