Skip to content

Commit be459cd

Browse files
committed
代码优化
1 parent 9533a4c commit be459cd

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

app/src/main/java/com/lxj/androidktxdemo/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ data class RestResult(
3131

3232
class MainActivity : BaseActivity() {
3333
val pages = arrayListOf(
34+
PageInfo("RecyclerView相关", RecyclerViewExtDemo()),
3435
PageInfo("Span相关", SpanExtPage()),
3536
PageInfo("View相关", ViewExtPage()),
3637
PageInfo("ImageView相关", ImageViewExtPage()),
3738
PageInfo("Fragment相关", FragmentExtPage()),
3839
PageInfo("Http相关", HttpExtFragment()),
3940
PageInfo("LiveDataBus", LiveDataBusDemo()),
40-
PageInfo("RecyclerView相关", RecyclerViewExtDemo()),
4141
PageInfo("ViewPager2", ViewPager2Demo()),
4242
PageInfo("播放器", PlayerFragment()),
4343
PageInfo("Uploader", UploaderFragment()),

app/src/main/java/com/lxj/androidktxdemo/fragment/RecyclerViewExtDemo.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ class RecyclerViewExtDemo : BaseFragment() {
8989
moveBtn.click(50) {
9090
// if (data.isEmpty()) return@click
9191
val random = userVM.listData.value!!.deepCopy<ArrayList<User>>()
92+
random.add(User(name = "随机添加-${Random.nextInt(1000)}"))
9293
random.shuffle()
94+
9395
userVM.replaceList(random)
96+
// recyclerView.diffUpdate(UserDiffCallback(old, userVM.listData.value))
97+
}
98+
clearBtn.click(50) {
99+
// if (data.isEmpty()) return@click
100+
userVM.clear()
94101
// recyclerView.diffUpdate(UserDiffCallback(old, userVM.listData.value))
95102
}
96103
val header = TextView(context).apply {

app/src/main/java/com/lxj/androidktxdemo/fragment/UserDiffCallback.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import com.lxj.androidktxdemo.entity.User
3232
//}
3333
class UserDiffCallback(oldData: List<User>?, newData: List<User>?) : DiffCallback<User>(oldData, newData) {
3434
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
35-
if(oldData.isNullOrEmpty() || newData.isNullOrEmpty()) return false
3635
LogUtils.d("areItemsTheSame ${oldData!![oldItemPosition].id == newData!![newItemPosition].id}")
3736
return oldData!![oldItemPosition].id == newData!![newItemPosition].id
3837
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<ImageView
1616
android:id="@+id/image"
17+
android:background="#aaa"
1718
android:layout_width="36dp"
1819
android:layout_height="36dp" />
1920

@@ -22,7 +23,7 @@
2223
android:textSize="16sp"
2324
android:textColor="#333"
2425
android:layout_marginLeft="12dp"
25-
android:text=""
26+
android:text="text"
2627
android:layout_width="wrap_content"
2728
android:layout_height="wrap_content" />
2829
</LinearLayout>

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,41 @@
33
android:layout_width="match_parent"
44
android:orientation="vertical"
55
android:layout_height="match_parent">
6-
<LinearLayout
7-
android:orientation="horizontal"
8-
android:layout_width="match_parent"
6+
<com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto"
7+
android:layout_width="wrap_content"
8+
app:flexWrap="wrap"
99
android:layout_height="wrap_content">
10-
1110
<Button
1211
android:id="@+id/addBtn"
1312
android:text="增加"
14-
android:layout_width="0dp"
15-
android:layout_weight="1"
13+
android:layout_width="wrap_content"
1614
android:layout_height="wrap_content" />
1715
<Button
1816
android:id="@+id/delBtn"
1917
android:text="减去"
20-
android:layout_width="0dp"
21-
android:layout_weight="1"
18+
android:layout_width="wrap_content"
2219
android:layout_height="wrap_content" />
2320
<Button
2421
android:id="@+id/updateBtn"
25-
android:text="更新字段"
26-
android:layout_width="0dp"
27-
android:layout_weight="1"
22+
android:text="局部更新字段"
23+
android:layout_width="wrap_content"
2824
android:layout_height="wrap_content" />
2925
<Button
3026
android:id="@+id/replaceBtn"
31-
android:text="替换"
32-
android:layout_width="0dp"
33-
android:layout_weight="1"
27+
android:text="整个替换Item"
28+
android:layout_width="wrap_content"
3429
android:layout_height="wrap_content" />
3530
<Button
3631
android:id="@+id/moveBtn"
3732
android:text="移动"
38-
android:layout_width="0dp"
39-
android:layout_weight="1"
33+
android:layout_width="wrap_content"
4034
android:layout_height="wrap_content" />
41-
42-
</LinearLayout>
35+
<Button
36+
android:id="@+id/clearBtn"
37+
android:text="清空"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content" />
40+
</com.google.android.flexbox.FlexboxLayout>
4341
<com.lxj.statelayout.StateLayout
4442
android:id="@+id/stateRv"
4543
android:layout_width="match_parent"

library/src/main/java/com/lxj/androidktx/base/ListVM.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,17 @@ abstract class ListVM<T>() : ViewModel(){
112112
}
113113

114114
fun replaceList(newList: List<T>){
115+
val list = listData.value ?: return
115116
updateOldData()
116-
listData.value?.clear()
117-
listData.value?.addAll(newList)
118-
listData.postValueAndSuccess(listData.value?: CopyOnWriteArrayList())
117+
list.clear()
118+
if(newList.isNullOrEmpty()){
119+
listData.postEmpty(list)
120+
}else{
121+
list.addAll(newList)
122+
listData.postValueAndSuccess(list)
123+
}
119124
}
120125

121-
122126
/**
123127
* 删除数据
124128
*/

0 commit comments

Comments
 (0)