【RecyclerView】 十一、RecyclerView 數(shù)據(jù)更新 ( 刪除單條數(shù)據(jù) | 批量刪除數(shù)據(jù) )
文章目錄

一、刪除單條數(shù)據(jù)
二、批量刪除數(shù)據(jù)
三、完整代碼示例
四、RecyclerView 相關(guān)資料
一、刪除單條數(shù)據(jù)
刪除單條數(shù)據(jù) : 調(diào)用 RecyclerView.Adapter 的 void notifyItemRemoved(int position) 方法 , 傳入的參數(shù)是被刪除元素的索引 ( 刪除之前的索引值 ) ;
該方法的作用是通知任何被注冊(cè)的觀察者 , position 位置的 item 元素對(duì)應(yīng)的數(shù)據(jù)被從數(shù)據(jù)集中刪除 ;
該位置之后的元素 ( 原來的位置是 oldPosition ) 目前在 oldPosition - 1 位置 ;
int position 參數(shù) : 當(dāng)前 RecyclerView 列表中被移出的 item 元素索引 , 也就是說之前的數(shù)據(jù)集合中被移出的數(shù)據(jù)索引 ;
注意 : 調(diào)用該方法后 , 只會(huì)刷新與該位置相關(guān)的 item 元素 , 不會(huì)刷新其它元素 , 即使數(shù)據(jù)改變了 , 也不會(huì)刷新 ;
代碼示例 : 刪除第 0 0 0 個(gè)元素 , 后續(xù)元素會(huì)依次遞進(jìn)補(bǔ)充上去 , 有相應(yīng)的動(dòng)畫觸發(fā) ;
// 刪除第 0 個(gè)元素 names.remove(0); // 通知適配器 adapter.notifyItemRemoved(0);
1
2
3
4
5
RecyclerView.Adapter.notifyItemRemoved(int position) 函數(shù)原型 : 該函數(shù)定義在 RecyclerView 的內(nèi)部類 Adapter 中 ;
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2, NestedScrollingChild3 { public abstract static class Adapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
運(yùn)行效果 : 動(dòng)畫效果是系統(tǒng)自帶的 , 可以自己實(shí)現(xiàn) ;
二、批量刪除數(shù)據(jù)
批量刪除數(shù)據(jù) : 調(diào)用 RecyclerView.Adapter 的 void notifyItemRangeInserted(int positionStart, int itemCount) 方法 , 傳入的參數(shù)是被刪除的元素的首個(gè)索引 , 以及插入元素的個(gè)數(shù) ;
該方法的作用是通知任何被注冊(cè)的觀察者 , 從 positionStart 位置開始的的 itemCount 個(gè) item 元素對(duì)應(yīng)的數(shù)據(jù)被從數(shù)據(jù)集中刪除 ;
該位置之后的元素 ( 原來的位置是 oldPosition ) 目前在 oldPosition - itemCount 位置 ;
參數(shù)說明 :
int positionStart 參數(shù) : 被刪除的元素在原數(shù)據(jù)集中首個(gè)元素的位置索引 ; ( 舊的數(shù)據(jù)集中的索引位置 )
int itemCount 參數(shù) : 數(shù)據(jù)集中被刪除元素個(gè)數(shù) ;
注意 : 調(diào)用該方法后 , 只會(huì)刷新與該位置相關(guān)的 item 元素 , 不會(huì)刷新其它元素 , 即使數(shù)據(jù)改變了 , 也不會(huì)刷新 ; 也就是說 RecyclerView 只刷新涉及到的 positionStart ~ positionStart + itemCount 之間的這幾個(gè)元素 , RecyclerView 中的其它 item 元素不變 ;
代碼示例 : 刪除前 3 3 3 個(gè)數(shù)據(jù) , 通知適配器 , 原數(shù)據(jù)集中的從第 0 0 0 個(gè)元素開始的 3 3 3 個(gè)數(shù)據(jù)元素被刪除了 ;
// 刪除第 0 ~ 2 個(gè)元素 names.remove(0); names.remove(0); names.remove(0); // 通知適配器 adapter.notifyItemRangeRemoved(0, 3);
1
2
3
4
5
6
7
RecyclerView.Adapter.notifyItemRangeRemoved(int positionStart, int itemCount) 函數(shù)原型 : 該函數(shù)定義在 RecyclerView 的內(nèi)部類 Adapter 中 ;
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2, NestedScrollingChild3 { public abstract static class Adapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
運(yùn)行效果 : 動(dòng)畫效果是系統(tǒng)自帶的 , 可以自己實(shí)現(xiàn) ;
三、完整代碼示例
完整代碼示例 僅做參考 :
package kim.hsl.recyclerview; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { /** * 數(shù)據(jù)源 */ private ArrayList
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
四、RecyclerView 相關(guān)資料
官方文檔 :
使用 RecyclerView 創(chuàng)建動(dòng)態(tài)列表 : https://developer.android.google.cn/guide/topics/ui/layout/recyclerview
高級(jí) RecyclerView 自定義 : https://developer.android.google.cn/guide/topics/ui/layout/recyclerview-custom
RecyclerView 官方文檔 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView
RecyclerView.Adapter 官方文檔 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.Adapter
RecyclerView.ViewHolder 官方文檔 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder
RecyclerView.ItemDecoration 官方文檔 : https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration
代碼示例 :
GitHub 源碼地址 : https://github.com/han1202012/001_RecyclerView
博客源碼快照 : https://download.csdn.net/download/han1202012/14984775
( 使用 Android Studio 打開 )
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。