Android Compose Column列表不自動(dòng)刷新問(wèn)題
1. 背景
我們都知道,Compose可以使用mutableStateOf和UI進(jìn)行綁定,改變值之后,就可以改變UI。
var value by remember { mutableStateOf(0) }
var imageVisible by remember { mutableStateOf(true) }
Column {
Text(text = "現(xiàn)在的值是:$value")
Button(onClick = {
value++ //修改值,自動(dòng)改變UI
}) {
Text(text = "Add Value")
}
AnimatedVisibility(visible = imageVisible) {
Image(
painter = painterResource(id = R.mipmap.photot1),
contentDescription = "",
Modifier.width(260.dp)
)
}
Button(onClick = {
imageVisible = !imageVisible //修改值,自動(dòng)顯示/隱藏UI
}) {
Text(text = "Show/Hide")
}
}
效果如下

但是如果是使用Column/Row/LazyColumn/LazyRow列表的時(shí)候,無(wú)論怎么更新數(shù)據(jù),界面都不會(huì)刷新
val list = ArrayList<String>()
for (i in 0..10) {
list.add(i.toString())
}
var stateList by remember { mutableStateOf(list) }
Button(onClick = {
stateList.add("添加的值:${Random.nextInt()}")
}, modifier = Modifier.fillMaxWidth()) {
Text(text = "添加值")
}
Button(onClick = {
stateList.removeAt(stateList.size - 1)
}, modifier = Modifier.fillMaxWidth()) {
Text(text = "刪除值")
}
LazyColumn {
items(stateList.size) { index ->
Text(
text = "${stateList.get(index)}",
textAlign = TextAlign.Center,
modifier = Modifier
.height(24.dp)
.fillMaxWidth()
)
}
}
可以看到,點(diǎn)擊了按鈕后,列表完全沒(méi)有刷新

這是為什么了 ?
2. 解決方案
當(dāng)時(shí)很不解,為啥其他類(lèi)型都是可以的,使用List就不行了呢 ?
查閱了好久,終于找到了解決方案
把mutableStateOf改用mutableStateListOf就可以了
var stateList = remember { mutableStateListOf<String>() }
for (i in 0..10) {
stateList.add(i.toString())
}
Button(onClick = {
stateList.add("添加的值:${Random.nextInt()}")
}, modifier = Modifier.fillMaxWidth()) {
Text(text = "添加值")
}
Button(onClick = {
stateList.removeAt(stateList.size - 1)
}, modifier = Modifier.fillMaxWidth()) {
Text(text = "刪除值")
}
LazyColumn {
items(stateList.size) { index ->
Text(
text = "${stateList.get(index)}",
textAlign = TextAlign.Center,
modifier = Modifier
.height(24.dp)
.fillMaxWidth()
)
}
}

3. 原因
解決方案很簡(jiǎn)單,但是這是為什么呢 ?
3.1 mutableStateOf為什么可以更新UI
我們以mutableStateOf()這個(gè)為例
var value by mutableStateOf(0)
首先,我們要明白,mutableStateOf()返回的是一個(gè)MutableState對(duì)象,MutableState中有一個(gè)var value: T屬性
interface MutableState<T> : State<T> {
override var value: T
operator fun component1(): T
operator fun component2(): (T) -> Unit
}
interface State<out T> {
val value: T
}
查看mutableStateOf源碼,可以發(fā)現(xiàn),mutableStateOf()返回的是繼承自MutableState的SnapshotMutableState對(duì)象,路徑mutableStateOf()-> createSnapshotMutableState() -> ParcelableSnapshotMutableState-> SnapshotMutableStateImpl,可以看到有這樣一段代碼
override var value: T
get() = next.readable(this).value
set(value) = next.withCurrent {
if (!policy.equivalent(it.value, value)) {
next.overwritable(this, it) { this.value = value }
}
}
private var next: StateStateRecord<T> = StateStateRecord(value)
這里就是重點(diǎn),SnapshotMutableStateImpl的value屬性重寫(xiě)了get()和set()方法
- 當(dāng)
value被讀的時(shí)候,不光把值返回,還會(huì)記錄一下在哪被讀的 - 當(dāng)
value被寫(xiě)的時(shí)候,不止把這個(gè)值給改了,還會(huì)去查找在哪里被讀過(guò),然后通知這些被讀過(guò)的地方,通知UI進(jìn)行刷新
4. 結(jié)論
因?yàn)槲覀儾僮?code>String、Int等基礎(chǔ)類(lèi)型的時(shí)候,都是通過(guò)get、set()來(lái)獲取、設(shè)置數(shù)據(jù)的,所以這操作會(huì)被SnapshotMutableStateImpl記錄下來(lái),而List、Map這種集合,我們是通過(guò)add、remove來(lái)更新數(shù)據(jù)的,所以不會(huì)觸發(fā)SnapshotMutableStateImpl value屬性的set。
4.1 解決方案一
使用mutableStateListOf替代mutableStateOf,mutableStateListOf內(nèi)部對(duì)add、remove方法也進(jìn)行了重寫(xiě)
4.2 解決方案二
新創(chuàng)建一個(gè)List,然后賦值給原來(lái)的list,這樣就會(huì)觸發(fā)set了
var stateList by remember { mutableStateOf(list) }
val tempList = ArrayList<String>()
for (value in stateList) {
tempList.add(value)
}
tempList.add("添加的值:${Random.nextInt()}")
stateList = tempList //賦值的時(shí)候會(huì)觸發(fā)刷新UI
5.自己實(shí)現(xiàn)一個(gè)mutableStateOf()
我們也可以自己來(lái)實(shí)現(xiàn)一個(gè)mutableStateOf,偽代碼如下
class Test {
interface State<out T> {
val value: T
}
interface MutableState<T> : State<T> {
override var value: T
/*operator fun component1(): T
operator fun component2(): (T) -> Unit*/
}
inline operator fun <T> State<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
inline operator fun <T> MutableState<T>.setValue(
thisObj: Any?,
property: KProperty<*>,
value: T
) {
this.value = value
}
interface SnapshotMutableState<T> : MutableState<T> {
val policy: SnapshotMutationPolicy<T>
}
interface SnapshotMutationPolicy<T> {
fun equivalent(a: T, b: T): Boolean
fun merge(previous: T, current: T, applied: T): T? = null
}
internal open class SnapshotMutableStateImpl<T>(
val _value: T,
override val policy: SnapshotMutationPolicy<T>
) : /*StateObject, */SnapshotMutableState<T> {
private var next : T = 52 as T
@Suppress("UNCHECKED_CAST")
override var value: T
get() = next
/*get() {
Log.i(TAGs.TAG, "getValue:$field")
return "" as T
}*/
set(value) {
Log.i(TAGs.TAG, "setValue")
this.value = value
}
/*override fun component1(): T {
//TODO("Not yet implemented")
}
override fun component2(): (T) -> Unit {
//TODO("Not yet implemented")
}*/
}
internal class ParcelableSnapshotMutableState<T>(
value: T,
policy: SnapshotMutationPolicy<T>
) : SnapshotMutableStateImpl<T>(value, policy)/*, Parcelable*/ {
}
fun <T> mutableStateOf(
value: T,
policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy()
): MutableState<T> = createSnapshotMutableState(value, policy)
fun <T> structuralEqualityPolicy(): SnapshotMutationPolicy<T> =
StructuralEqualityPolicy as SnapshotMutationPolicy<T>
private object StructuralEqualityPolicy : SnapshotMutationPolicy<Any?> {
override fun equivalent(a: Any?, b: Any?) = a == b
override fun toString() = "StructuralEqualityPolicy"
}
fun <T> createSnapshotMutableState(
value: T,
policy: SnapshotMutationPolicy<T>
): SnapshotMutableState<T> = ParcelableSnapshotMutableState(value, policy)
fun main() {
var sizeUpdate by mutableStateOf(48)
Log.i(TAGs.TAG, "sizeUpdate:$sizeUpdate")
sizeUpdate = 64
Log.i(TAGs.TAG, "sizeUpdate>>$sizeUpdate")
}
}
到此這篇關(guān)于Android Compose Column列表不自動(dòng)刷新問(wèn)題的文章就介紹到這了,更多相關(guān)Android Compose Column內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android實(shí)現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實(shí)現(xiàn)視頻的加密和解密(使用AES),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
詳解 Kotlin Reference Basic Types, String, Array and Imports
這篇文章主要介紹了詳解 Kotlin Reference Basic Types, String, Array and Imports的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android自定義View Material Design理念詳解
這篇文章主要為大家介紹了Android自定義View Material Design理念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android統(tǒng)一處理登錄后攜帶數(shù)據(jù)跳轉(zhuǎn)到目標(biāo)頁(yè)面的方式
我們?cè)陂_(kāi)發(fā)的時(shí)候,一定會(huì)遇到頁(yè)面跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一處理登錄后攜帶數(shù)據(jù)跳轉(zhuǎn)到目標(biāo)頁(yè)面的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法
這篇文章主要介紹了Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法,本文講解了RGB值轉(zhuǎn)Bitmap、Color值轉(zhuǎn)Bitmap、字節(jié)數(shù)組轉(zhuǎn)Bitmap、讀取文件轉(zhuǎn)Bitmap、讀取資源轉(zhuǎn)Bitmap、輸入流轉(zhuǎn)Bitmap等內(nèi)容,需要的朋友可以參考下2015-01-01
android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信
本篇文章主要介紹了android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-04-04

