Skip to content

Commit 20577d4

Browse files
committed
增加bindWithPayloads
1 parent 1a79978 commit 20577d4

File tree

10 files changed

+208
-23
lines changed

10 files changed

+208
-23
lines changed

app/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ android {
2121

2222
dependencies {
2323
implementation fileTree(dir: 'libs', include: ['*.jar'])
24-
implementation 'androidx.appcompat:appcompat:1.1.0'
25-
implementation 'androidx.recyclerview:recyclerview:1.1.0'
24+
implementation 'androidx.appcompat:appcompat:1.3.1'
25+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
2626
implementation project(":easy-adapter")
2727
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
28+
implementation 'com.blankj:utilcodex:1.31.0'
29+
implementation 'com.google.code.gson:gson:2.8.6'
30+
implementation 'com.github.bumptech.glide:glide:4.12.0'
2831
}
2932

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.lxj.easyadapter.sample">
4-
4+
<uses-permission android:name="android.permission.INTERNET"/>
55
<application
66
android:allowBackup="true"
77
android:icon="@mipmap/ic_launcher"

app/src/main/java/com/lxj/easyadapter/sample/MainActivity.kt

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,94 @@ import android.graphics.Color
44
import android.os.Bundle
55
import android.view.View
66
import android.view.ViewGroup
7+
import android.widget.ImageView
78
import android.widget.TextView
89
import android.widget.Toast
910
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.recyclerview.widget.DiffUtil
1012
import androidx.recyclerview.widget.LinearLayoutManager
1113
import androidx.recyclerview.widget.RecyclerView
14+
import com.blankj.utilcode.util.CloneUtils
15+
import com.blankj.utilcode.util.GsonUtils
16+
import com.bumptech.glide.Glide
17+
import com.bumptech.glide.load.engine.DiskCacheStrategy
18+
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
19+
import com.bumptech.glide.request.RequestOptions
20+
import com.google.gson.GsonBuilder
21+
import com.google.gson.reflect.TypeToken
1222
import com.lxj.easyadapter.*
1323

1424
import kotlinx.android.synthetic.main.activity_main.*
25+
import java.lang.reflect.Type
1526
import java.util.*
1627
import java.util.concurrent.CopyOnWriteArrayList
28+
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory
29+
30+
31+
1732

1833
class MainActivity : AppCompatActivity() {
1934

2035
internal var userList: MutableList<User> = ArrayList()
2136
private var adapter: EasyAdapter<User>? = null
2237
private var multiItemTypeAdapter: MultiItemTypeAdapter<User>? = null
23-
38+
val url = "https://img1.baidu.com/it/u=1925715390,133119052&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
2439
override fun onCreate(savedInstanceState: Bundle?) {
2540
super.onCreate(savedInstanceState)
2641
setContentView(R.layout.activity_main)
42+
userList.add(User(name = "盛大"))
43+
userList.add(User(name = "都是"))
44+
userList.add(User(name = "CEAD"))
45+
userList.add(User(name = "大图"))
46+
add.setOnClickListener {
47+
val old = deepCopy()
48+
val range = (0 until userList.size)
49+
userList.add(if(range.isEmpty()) 0 else range.random(), User(id = UUID.randomUUID().toString(), name = "随机添加 - ${(0..1000).random()}" ))
50+
DiffUtil.calculateDiff(UserDiffCallback(old, userList)).dispatchUpdatesTo(recyclerView.adapter!!)
51+
}
52+
del.setOnClickListener {
53+
if(userList.isNullOrEmpty()) return@setOnClickListener
54+
val range = (0 until userList.size)
55+
val old = deepCopy()
56+
userList.removeAt(range.random())
57+
DiffUtil.calculateDiff(UserDiffCallback(old, userList)).dispatchUpdatesTo(recyclerView.adapter!!)
58+
}
59+
update.setOnClickListener {
60+
if(userList.isNullOrEmpty()) return@setOnClickListener
61+
val index = (0 until userList.size).random()
62+
val old = deepCopy()
63+
userList[index].name += " - 随机更新"
64+
DiffUtil.calculateDiff(UserDiffCallback(old, userList)).dispatchUpdatesTo(recyclerView.adapter!!)
65+
}
66+
replace.setOnClickListener {
67+
if(userList.isNullOrEmpty()) return@setOnClickListener
68+
val index = (0 until userList.size).random()
69+
val old = deepCopy()
70+
userList[index] = User(name = "我是随机替换的")
71+
DiffUtil.calculateDiff(UserDiffCallback(old, userList)).dispatchUpdatesTo(recyclerView.adapter!!)
72+
}
73+
move.setOnClickListener {
74+
if(userList.isNullOrEmpty()) return@setOnClickListener
75+
val old = deepCopy()
76+
userList.reverse()
77+
DiffUtil.calculateDiff(UserDiffCallback(old, userList)).dispatchUpdatesTo(recyclerView.adapter!!)
78+
}
2779
recyclerView.layoutManager = LinearLayoutManager(this)
28-
recyclerView.itemAnimator = null
2980

3081
//prepare data
31-
for (i in 0..6) {
32-
userList.add(User("本杰明 - $i", i * 2, i))
33-
}
82+
// for (i in 0..6) {
83+
// userList.add(User("本杰明 - $i", i * 2, i))
84+
// }
3485

3586
// testHeader()
3687
testMultiItem()
3788
}
3889

90+
fun deepCopy(): List<User>{
91+
val json = GsonBuilder().create().toJson(userList)
92+
return GsonBuilder().create().fromJson<List<User>>(json, object : TypeToken<List<User>>() {}.type)
93+
}
94+
3995
private fun testHeader() {
4096
adapter = object : EasyAdapter<User>(userList, R.layout.item) {
4197
override fun bind(holder: ViewHolder, user: User, position: Int) {
@@ -88,10 +144,10 @@ class MainActivity : AppCompatActivity() {
88144
multiItemTypeAdapter = MultiItemTypeAdapter<User>(userList)
89145
.apply {
90146
addItemDelegate(OneDelegate())
91-
addItemDelegate(TwoDelegate())
92-
addHeaderView(createView("Multi Header view1111"))
93-
addHeaderView(createView("Multi Header view22222"))
94-
addFootView(createView("Multi Footer view"))
147+
// addItemDelegate(TwoDelegate())
148+
// addHeaderView(createView("Multi Header view1111"))
149+
// addHeaderView(createView("Multi Header view22222"))
150+
// addFootView(createView("Multi Footer view"))
95151
setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
96152
override fun onItemClick(view: View, holder: RecyclerView.ViewHolder, position: Int) {
97153
Toast.makeText(this@MainActivity, "position: $position", Toast.LENGTH_SHORT).show()
@@ -108,13 +164,35 @@ class MainActivity : AppCompatActivity() {
108164
internal inner class OneDelegate : ItemDelegate<User> {
109165

110166
override fun isThisType(item: User, position: Int): Boolean {
111-
return position % 2 != 0
167+
return true
168+
// return position % 2 != 0
112169
}
113170
override fun bind(holder: ViewHolder, user: User, position: Int) {
114-
holder.setText(android.R.id.text1, "name: " + user.name + " - " + position)
171+
holder.setText(R.id.name, user.name)
172+
Glide.with(this@MainActivity)
173+
.load(url).transition(DrawableTransitionOptions.withCrossFade(1000))
174+
.diskCacheStrategy(DiskCacheStrategy.NONE)
175+
.skipMemoryCache(true)
176+
.placeholder(R.mipmap.ic_launcher_round)
177+
.into(holder.getView<ImageView>(R.id.avatar))
178+
}
179+
180+
//布局更新
181+
override fun bindWithPayloads(
182+
holder: ViewHolder,
183+
t: User,
184+
position: Int,
185+
payloads: List<Any>
186+
) {
187+
if(payloads.isNullOrEmpty()) return
188+
val bundle = payloads[0] as Bundle
189+
val name = bundle.getString("name") ?: ""
190+
if(!name.isNullOrEmpty()){
191+
holder.setText(R.id.name, name)
192+
}
115193
}
116194

117-
override fun getLayoutId(): Int = android.R.layout.simple_list_item_1
195+
override fun getLayoutId(): Int = R.layout.adapter_one
118196
}
119197

120198
internal inner class TwoDelegate : ItemDelegate<User> {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package com.lxj.easyadapter.sample
22

3+
import java.util.*
34

4-
data class User(var name: String = "", var age: Int = 0, var id: Int = 0)
5+
6+
data class User(var name: String = "", var age: Int = 0, var id: String = UUID.randomUUID().toString())
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.lxj.easyadapter.sample
2+
3+
import android.os.Bundle
4+
import androidx.recyclerview.widget.DiffUtil
5+
6+
class UserDiffCallback(var oldData: List<User>?, var newData: List<User>?) : DiffUtil.Callback() {
7+
override fun getOldListSize() = oldData?.size ?: 0
8+
override fun getNewListSize() = newData?.size ?: 0
9+
10+
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
11+
if(oldData.isNullOrEmpty() || newData.isNullOrEmpty()) return false
12+
return oldData!![oldItemPosition].id == newData!![newItemPosition].id
13+
}
14+
15+
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
16+
return oldData!![oldItemPosition].name == newData!![newItemPosition].name
17+
}
18+
19+
//局部更新 areItemsTheSame为true && areContentsTheSame==false 调用
20+
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
21+
val oldItem = oldData!![oldItemPosition]
22+
val newItem = newData!![newItemPosition]
23+
val bundle = Bundle()
24+
if(oldItem.name != newItem.name){
25+
bundle.putString("name", newItem.name)
26+
}
27+
return bundle
28+
}
29+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,46 @@
55
android:orientation="vertical">
66

77

8+
<LinearLayout
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content">
11+
12+
<Button
13+
android:id="@+id/add"
14+
android:text="添加"
15+
android:layout_width="0dp"
16+
android:layout_weight="1"
17+
android:layout_height="wrap_content"/>
18+
<Button
19+
android:id="@+id/del"
20+
android:text="删除"
21+
android:layout_width="0dp"
22+
android:layout_weight="1"
23+
android:layout_height="wrap_content"/>
24+
<Button
25+
android:id="@+id/update"
26+
android:text="更新"
27+
android:layout_width="0dp"
28+
android:layout_weight="1"
29+
android:layout_height="wrap_content"/>
30+
<Button
31+
android:id="@+id/replace"
32+
android:text="替换"
33+
android:layout_width="0dp"
34+
android:layout_weight="1"
35+
android:layout_height="wrap_content"/>
36+
<Button
37+
android:id="@+id/move"
38+
android:text="移动"
39+
android:layout_width="0dp"
40+
android:layout_weight="1"
41+
android:layout_height="wrap_content"/>
842

43+
</LinearLayout>
944
<androidx.recyclerview.widget.RecyclerView
1045
android:id="@+id/recyclerView"
1146
android:layout_width="match_parent"
12-
android:layout_height="wrap_content"
47+
android:layout_height="match_parent"
1348
android:splitMotionEvents="false" />
1449
</LinearLayout>
1550

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="horizontal"
4+
android:padding="12dp"
5+
android:gravity="center_vertical"
6+
android:layout_width="match_parent"
7+
android:layout_height="wrap_content">
8+
9+
<ImageView
10+
android:id="@+id/avatar"
11+
android:layout_width="40dp"
12+
android:layout_height="40dp"/>
13+
<TextView
14+
android:id="@+id/name"
15+
android:layout_marginLeft="10dp"
16+
android:textColor="#222222"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"/>
19+
</LinearLayout>

easy-adapter/src/main/java/com/lxj/easyadapter/ItemDelegate.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ interface ItemDelegate<T> {
1111

1212
fun bind(holder: ViewHolder, t: T, position: Int)
1313

14+
fun bindWithPayloads(holder: ViewHolder, t: T, position: Int, payloads: List<Any>) {
15+
bind(holder, t, position)
16+
}
17+
18+
1419
}

easy-adapter/src/main/java/com/lxj/easyadapter/ItemDelegateManager.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,22 @@ class ItemDelegateManager<T> {
6161
"No ItemDelegate added that matches position=$position in data source")
6262
}
6363

64-
fun convert(holder: ViewHolder, item: T, position: Int) {
64+
fun convert(holder: ViewHolder, item: T, position: Int, payloads: List<Any>? = null) {
6565
val delegatesCount = delegates.size()
6666
for (i in 0 until delegatesCount) {
6767
val delegate = delegates.valueAt(i)
6868

6969
if (delegate.isThisType(item, position)) {
70-
delegate.bind(holder, item, position)
70+
if (payloads.isNullOrEmpty()){
71+
delegate.bind(holder, item, position)
72+
}else{
73+
delegate.bindWithPayloads(holder, item, position, payloads)
74+
}
7175
return
7276
}
7377
}
74-
throw IllegalArgumentException(
75-
"No ItemDelegateManager added that matches position=$position in data source")
78+
// throw IllegalArgumentException(
79+
// "No ItemDelegateManager added that matches position=$position in data source")
7680
}
7781

7882

easy-adapter/src/main/java/com/lxj/easyadapter/MultiItemTypeAdapter.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ open class MultiItemTypeAdapter<T>(var data: List<T>) : RecyclerView.Adapter<Vie
5353

5454
fun onViewHolderCreated(holder: ViewHolder, itemView: View) { }
5555

56-
fun convert(holder: ViewHolder, t: T) {
57-
mItemDelegateManager.convert(holder, t, holder.adapterPosition - headersCount)
56+
fun convert(holder: ViewHolder, t: T, payloads: List<Any>? = null) {
57+
mItemDelegateManager.convert(holder, t, holder.adapterPosition - headersCount, payloads)
5858
}
5959

6060
protected fun isEnabled(viewType: Int): Boolean {
@@ -90,6 +90,16 @@ open class MultiItemTypeAdapter<T>(var data: List<T>) : RecyclerView.Adapter<Vie
9090
convert(holder, data[position - headersCount])
9191
}
9292

93+
override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: List<Any>) {
94+
if (isHeaderViewPos(position)) {
95+
return
96+
}
97+
if (isFooterViewPos(position)) {
98+
return
99+
}
100+
convert(holder, data[position - headersCount], payloads)
101+
}
102+
93103
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
94104
super.onAttachedToRecyclerView(recyclerView)
95105
WrapperUtils.onAttachedToRecyclerView(

0 commit comments

Comments
 (0)