【RecyclerView】 十、RecyclerView 數(shù)據(jù)更新 ( 增加單條數(shù)據(jù) | 批量增加數(shù)據(jù) )
文章目錄
一、增加單條數(shù)據(jù)
二、增加多條數(shù)據(jù)
三、完整代碼示例
四、RecyclerView 相關(guān)資料
一、增加單條數(shù)據(jù)
增加單條數(shù)據(jù) : 調(diào)用 RecyclerView.Adapter 的 void notifyItemInserted(int position) 方法 , 傳入的參數(shù)是被插入的元素的當(dāng)前索引 ;
該方法的作用是通知任何被注冊(cè)的觀察者 , 該 item 元素的 position 位置是被新插入的 ;
該位置之前的元素目前在 position + 1 位置 ;
int position 參數(shù) : 最新插入的元素在數(shù)據(jù)集中的位置 ;
注意 : 調(diào)用該方法后 , 只會(huì)刷新與該位置相關(guān)的 item 元素 , 不會(huì)刷新其它元素 , 即使數(shù)據(jù)改變了 , 也不會(huì)刷新 ;
代碼示例 : 在集合的開(kāi)始位置插入一個(gè)元素 ;
// 在集合開(kāi)始添加一個(gè)元素 names.add(0, "戴宗"); // 通知適配器新元素添加 adapter.notifyItemInserted(0);
1
2
3
4
5
RecyclerView.Adapter.notifyItemInserted(int position) 函數(shù)原型 : 該函數(shù)定義在 RecyclerView 的內(nèi)部類(lè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
運(yùn)行效果 : 動(dòng)畫(huà)效果是系統(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 位置開(kāi)始的 itemCount 個(gè) item 元素是被新插入的 ;
被插入數(shù)據(jù)之前 positionStart 位置的元素目前在 positionStart + itemCount 位置 ;
參數(shù)說(shuō)明 :
int positionStart 參數(shù) : 新插入的首個(gè)元素位置 ;
int itemCount 參數(shù) : 新插入元素的個(gè)數(shù) ;
代碼示例 : 在集合的開(kāi)始位置插入三個(gè)元素 ;
// 在集合開(kāi)始添加多個(gè)元素 names.add(0, "戴宗"); names.add(0, "劉唐"); names.add(0, "李逵"); // 通知適配器多個(gè)新元素添加 adapter.notifyItemRangeInserted(0, 3);
1
2
3
4
5
6
7
RecyclerView.Adapter.notifyItemInserted(int position) 函數(shù)原型 : 該函數(shù)定義在 RecyclerView 的內(nèi)部類(lè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)畫(huà)效果是系統(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 打開(kāi) )
iOS
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(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ò)用戶(hù)投稿,版權(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)容。