Skip to content

Commit

Permalink
优化payload的更新方式
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuai1 committed Jun 18, 2019
1 parent cb20cd7 commit fd50a2a
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LOLActivity : AppCompatActivity(){
diffAdapter.registerHolder(LegendHolder::class.java, LegendViewData.VIEW_ID)


val linearLayoutManager = LinearLayoutManager(this)
val linearLayoutManager = LinearLayoutManagerWrapper(this)
mRVTest!!.layoutManager = linearLayoutManager
mRVTest!!.adapter = diffAdapter

Expand Down Expand Up @@ -173,7 +173,7 @@ class LOLActivity : AppCompatActivity(){

it.post(object :Runnable{
override fun run() {
diffAdapter.clear()
// diffAdapter.clear()
val time = (30 until 450L).random()
it.postDelayed(this, time)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.silencedut.core.provider.legend.LegendNotification
import com.silencedut.core.provider.legend.pojo.*
import com.silencedut.diffadapter.DiffAdapter
import com.silencedut.diffadapter.data.BaseMutableData
import com.silencedut.diffadapter.utils.UpdatePayloadFunction
import com.silencedut.diffadapter.utils.UpdateFunction
import com.silencedut.diffadapterdemo.adapter.LegendViewData
import com.silencedut.diffadapterdemo.adapter.SkinViewData
Expand Down Expand Up @@ -90,15 +91,17 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
})

//如果变化的数据只需要特定类型的Holder刷新,类型即可指定
diffAdapter.addUpdateMediator(legendPriceData, object : UpdateFunction<LegendPrice, LegendViewData> {
override fun providerMatchFeature(input: LegendPrice): Any {
return input.id
diffAdapter.addUpdateMediator(legendPriceData, object : UpdatePayloadFunction<LegendPrice, LegendViewData>() {
override fun applyChange(
input: LegendPrice, originalData: LegendViewData, payloadKeys: MutableSet<String>
): LegendViewData {
originalData.price = input.price
payloadKeys.add(LegendViewData.KEY_PRICE)
return originalData
}

override fun applyChange(input: LegendPrice, originalData: LegendViewData): LegendViewData {
Log.d(TAG,"applyChange legendPriceData $input")
//可以new对象
return LegendViewData(originalData.id, originalData.legendBaseInfo, input.price)
override fun providerMatchFeature(input: LegendPrice): Any {
return input.id
}
})

Expand All @@ -124,7 +127,7 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific

override fun onLegendPriceFetched(legendPrice: LegendPrice) {
Log.d(TAG,"onLegendPriceFetched $legendPrice")
legendPriceData.value = legendPrice
legendPriceData.postValue(legendPrice)

}

Expand Down

0 comments on commit fd50a2a

Please sign in to comment.