diff --git a/compose-recyclerview/src/main/java/com/example/compose_recyclerview/adapter/ComposeRecyclerViewAdapter.kt b/compose-recyclerview/src/main/java/com/example/compose_recyclerview/adapter/ComposeRecyclerViewAdapter.kt index aa7f94c..31cb7dc 100644 --- a/compose-recyclerview/src/main/java/com/example/compose_recyclerview/adapter/ComposeRecyclerViewAdapter.kt +++ b/compose-recyclerview/src/main/java/com/example/compose_recyclerview/adapter/ComposeRecyclerViewAdapter.kt @@ -16,15 +16,13 @@ class ComposeRecyclerViewAdapter : fun getItemType(position: Int): Int } + private var itemList: MutableList = mutableListOf() + var totalItems: Int = 0 set(value) { if (field == value) return field = value - if (field == -1) { - notifyItemInserted(0) - } else { - notifyItemChanged(0) - } + notifyItemRangeChange(value) } var itemBuilder: (@Composable (index: Int) -> Unit)? = @@ -80,4 +78,16 @@ class ComposeRecyclerViewAdapter : this.itemTypeBuilder = it } } + + private fun notifyItemRangeChange(newSize: Int) { + val oldSize = itemList.size + if (newSize < oldSize) { + itemList = itemList.subList(0, newSize) + notifyItemRangeRemoved(newSize, oldSize - newSize) + } else if (newSize > oldSize) { + val list = MutableList(newSize - oldSize) { Any() } + itemList = (itemList + list).toMutableList() + notifyItemRangeInserted(oldSize, newSize - oldSize) + } + } } \ No newline at end of file