Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Jun 1, 2023
1 parent bff99b3 commit 2855a39
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class RecyclerViewExtDemo : BaseFragment() {
if(userVM.listData.value.isNullOrEmpty()) return@click
val randomPosition = Random.nextInt(userVM.listData.value!!.size)
val t = userVM.listData.value!![randomPosition].deepCopy<User>()
// val t = userVM.listData.value!![randomPosition]
t.name = "局部字段替换-${randomPosition}"
userVM.update(randomPosition, t)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lxj.androidktxdemo.fragment

import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import com.blankj.utilcode.util.LogUtils
import com.lxj.androidktx.core.DiffCallback
import com.lxj.androidktxdemo.entity.User

Expand Down Expand Up @@ -32,10 +33,12 @@ import com.lxj.androidktxdemo.entity.User
class UserDiffCallback(oldData: List<User>?, newData: List<User>?) : DiffCallback<User>(oldData, newData) {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
if(oldData.isNullOrEmpty() || newData.isNullOrEmpty()) return false
LogUtils.d("areItemsTheSame ${oldData!![oldItemPosition].id == newData!![newItemPosition].id}")
return oldData!![oldItemPosition].id == newData!![newItemPosition].id
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
LogUtils.d("areContentsTheSame ${oldData!![oldItemPosition].name == newData!![newItemPosition].name}")
return oldData!![oldItemPosition].name == newData!![newItemPosition].name
}

Expand All @@ -47,6 +50,7 @@ class UserDiffCallback(oldData: List<User>?, newData: List<User>?) : DiffCallbac
if(oldItem.name != newItem.name){
bundle.putString("name", newItem.name)
}
LogUtils.d("getChangePayload ${bundle}")
return bundle
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,22 @@ object ExoPlayerManager : CacheListener{

fun stop(){
stopPostProgress()
currentIndex = -1
uriList.clear()
val info = PlayInfo(index = currentIndex, current = 0,
total = 0, uri = "")
playInfo.setValue(info)
player.stop()
}

fun release(){
stop()
stopPostProgress()
currentIndex = -1
uriList.clear()
val info = PlayInfo(index = currentIndex, current = 0,
total = 0, uri = "")
playInfo.setValue(info)
player.release()
}

override fun onCacheAvailable(cacheFile: File, url: String, percentsAvailable: Int) {
Expand Down
5 changes: 4 additions & 1 deletion library/src/main/java/com/lxj/androidktx/base/ListVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.lxj.androidktx.core.*
import com.lxj.androidktx.livedata.StateLiveData
import com.lxj.statelayout.StateLayout
Expand Down Expand Up @@ -31,6 +32,7 @@ abstract class ListVM<T>() : ViewModel(){
onDataUpdate: (() -> Unit)? = null,
) {
listData.observe(owner, Observer {
LogUtils.d("listData observe")
firstLoad = false
val diffCallback = getDiffCallback(oldData, it)
if(diffCallback!=null){
Expand Down Expand Up @@ -103,7 +105,8 @@ abstract class ListVM<T>() : ViewModel(){

/**
* 更新数据,如果直接修改指定位置的bean,会同步更新old;导致old和new是一样的数据。
* 推荐将目标数据deepCopy()后再进行修改,这样不会同步更新old
* 注意:需要将目标数据deepCopy()后再进行修改,这样不会同步更新old,产生diff
* 如果直接修改原数据,则old数据也会修改,导致old和new数据一样,不会触发UI更新
*/
fun update(position: Int, t: T){
val list = listData.value!!
Expand Down

0 comments on commit 2855a39

Please sign in to comment.