-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
208 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package com.lxj.easyadapter.sample | ||
|
||
import java.util.* | ||
|
||
data class User(var name: String = "", var age: Int = 0, var id: Int = 0) | ||
|
||
data class User(var name: String = "", var age: Int = 0, var id: String = UUID.randomUUID().toString()) |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/lxj/easyadapter/sample/UserDiffCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.lxj.easyadapter.sample | ||
|
||
import android.os.Bundle | ||
import androidx.recyclerview.widget.DiffUtil | ||
|
||
class UserDiffCallback(var oldData: List<User>?, var newData: List<User>?) : DiffUtil.Callback() { | ||
override fun getOldListSize() = oldData?.size ?: 0 | ||
override fun getNewListSize() = newData?.size ?: 0 | ||
|
||
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { | ||
if(oldData.isNullOrEmpty() || newData.isNullOrEmpty()) return false | ||
return oldData!![oldItemPosition].id == newData!![newItemPosition].id | ||
} | ||
|
||
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { | ||
return oldData!![oldItemPosition].name == newData!![newItemPosition].name | ||
} | ||
|
||
//局部更新 areItemsTheSame为true && areContentsTheSame==false 调用 | ||
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { | ||
val oldItem = oldData!![oldItemPosition] | ||
val newItem = newData!![newItemPosition] | ||
val bundle = Bundle() | ||
if(oldItem.name != newItem.name){ | ||
bundle.putString("name", newItem.name) | ||
} | ||
return bundle | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="horizontal" | ||
android:padding="12dp" | ||
android:gravity="center_vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<ImageView | ||
android:id="@+id/avatar" | ||
android:layout_width="40dp" | ||
android:layout_height="40dp"/> | ||
<TextView | ||
android:id="@+id/name" | ||
android:layout_marginLeft="10dp" | ||
android:textColor="#222222" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"/> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters