Skip to content

Commit 3b2956e

Browse files
committed
修复java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 的崩溃
1 parent 774eba1 commit 3b2956e

File tree

7 files changed

+59
-41
lines changed

7 files changed

+59
-41
lines changed

app/src/main/java/com/silencedut/diffadapterdemo/ImageHolder.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ImageHolder( itemView: View, recyclerAdapter: DiffAdapter): BaseDiffViewHo
2626
}
2727

2828
override fun updateItem(data: ImageData, position: Int) {
29-
Log.d(TAG,"updateItem : "+context.resources.getResourceName(data.sourceId))
29+
Log.d(TAG,"updatePartWithPayload : "+context.resources.getResourceName(data.sourceId))
3030

3131

3232
imageView?.setImageResource(data.sourceId)

app/src/main/java/com/silencedut/diffadapterdemo/MainActivity.java

+24-24
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,30 @@ protected void onCreate(Bundle savedInstanceState) {
3636
mRVTest.setLayoutManager(linearLayoutManager);
3737
mRVTest.setAdapter(mDiffAdapter);
3838
scheduleUpdate();
39-
mRVTest.postDelayed(new Runnable() {
40-
@Override
41-
public void run() {
42-
scheduleUpdate();
43-
mRVTest.postDelayed(this,1000);
44-
45-
}
46-
},1000);
47-
48-
mRVTest.postDelayed(new Runnable() {
49-
@Override
50-
public void run() {
51-
52-
List<Long> uids = TestCode.Companion.getQueueUids();
53-
54-
for(int index =0 ;index <uids.size() ;index ++ ) {
55-
ModelProvider.getModel(MainActivity.this,DataChangeViewModel.class).getChangedTextSource().setValue(new DataSource(uids.get(index),R.drawable.ic_launcher_foreground,"ddd"));
56-
}
57-
58-
59-
mRVTest.postDelayed(this,800);
60-
61-
}
62-
},800);
39+
//// mRVTest.postDelayed(new Runnable() {
40+
//// @Override
41+
//// public void run() {
42+
//// scheduleUpdate();
43+
//// mRVTest.postDelayed(this,1000);
44+
////
45+
//// }
46+
//// },1000);
47+
//
48+
// mRVTest.postDelayed(new Runnable() {
49+
// @Override
50+
// public void run() {
51+
//
52+
// List<Long> uids = TestCode.Companion.getQueueUids();
53+
//
54+
// for(int index =0 ;index <uids.size() ;index ++ ) {
55+
// ModelProvider.getModel(MainActivity.this,DataChangeViewModel.class).getChangedTextSource().setValue(new DataSource(uids.get(index),R.drawable.ic_launcher_foreground,"ddd"));
56+
// }
57+
//
58+
//
59+
// mRVTest.postDelayed(this,800);
60+
//
61+
// }
62+
// },800);
6363

6464

6565

app/src/main/java/com/silencedut/diffadapterdemo/TextHolder.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class TextHolder(itemViewRoot: View, recyclerAdapter: DiffAdapter): BaseDiffView
2828
}
2929

3030
override fun updateItem(data: TextData, position: Int) {
31-
Log.d(TAG,"updateItem")
31+
Log.d(TAG,"updatePartWithPayload")
3232
textView?.text = data.content
3333
textView?.setBackgroundColor(data.backgroundColor)
3434
}
3535

36-
override fun updateItem(data: TextData, position: Int, payload: Bundle) {
36+
override fun updatePartWithPayload(position: Int, payload: Bundle) {
3737

38-
Log.d(TAG,"position :"+position+"updateItem payload content :"+payload.getString("content")+",backgroundColor : "+payload.getString("backgroundColor"))
38+
Log.d(TAG,"position :"+position+"updatePartWithPayload payload content :"+payload.getString("content")+",backgroundColor : "+payload.getString("backgroundColor"))
3939
if(payload.getString("content")!=null) {
4040
textView?.text = payload.getString("content")
4141
}

diffadapter/src/main/java/com/silencedut/diffadapter/DiffAdapter.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ public <T extends BaseMutableData> void addData(T data) {
187187
return;
188188
}
189189
mDatas.add(data);
190-
doNotifyUI();
190+
mDifferHelper.updateInnerList(mDatas);
191+
notifyItemChanged(mDatas.size()-1);
191192
}
192193

193194

@@ -200,7 +201,8 @@ public <T extends BaseMutableData> void addData(List<T> datas) {
200201
return;
201202
}
202203
mDatas.addAll(datas);
203-
doNotifyUI();
204+
mDifferHelper.updateInnerList(mDatas);
205+
notifyItemChanged(mDatas.size() - datas.size(),datas.size());
204206
}
205207

206208

@@ -229,7 +231,6 @@ public void updateData(BaseMutableData newData) {
229231

230232
}
231233
}
232-
doNotifyUI();
233234

234235
}
235236

@@ -239,14 +240,17 @@ public void deleteData(BaseMutableData data) {
239240
}
240241
Iterator<BaseMutableData> iterator = mDatas.iterator();
241242

243+
int position = -1;
242244
while (iterator.hasNext()) {
245+
position ++;
243246

244247
if(data.uniqueItemFeature().equals(iterator.next().uniqueItemFeature())) {
245248
iterator.remove();
249+
mDifferHelper.updateInnerList(mDatas);
250+
notifyItemRemoved(position);
246251
break;
247252
}
248253
}
249-
doNotifyUI();
250254

251255
}
252256

@@ -303,7 +307,7 @@ public void onBindViewHolder(@NonNull BaseDiffViewHolder baseDiffViewHolder, int
303307
try {
304308
baseDiffViewHolder.updateItem(mDifferHelper.getCurrentList().get(position), position);
305309
}catch (Exception e) {
306-
Log.e(TAG,"onBindViewHolder updateItem error",e);
310+
Log.e(TAG,"onBindViewHolder updatePartWithPayload error",e);
307311
}
308312
}
309313

@@ -323,9 +327,9 @@ public void onBindViewHolder(@NonNull BaseDiffViewHolder holder, int position, @
323327
this.onBindViewHolder(holder,position);
324328
}else {
325329
try {
326-
holder.updateItem(mDifferHelper.getCurrentList().get(position), position,(Bundle) payloads.get(0));
330+
holder.updatePartWithPayload(position,(Bundle) payloads.get(0));
327331
}catch (Exception e) {
328-
Log.e(TAG,"onBindViewHolder updateItem payload error",e);
332+
Log.e(TAG,"onBindViewHolder updatePartWithPayload payload error",e);
329333
}
330334
}
331335

diffadapter/src/main/java/com/silencedut/diffadapter/holder/BaseDiffViewHolder.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ public BaseDiffViewHolder(View itemView, DiffAdapter baseRecyclerAdapter) {
5151
setUIContext(mBaseAdapter.mContext);
5252
}
5353

54-
54+
/**
55+
* default update item way , which payload is empty
56+
* @param data
57+
* @param position
58+
*/
5559
public abstract void updateItem(@NonNull T data, int position);
5660

57-
public void updateItem(@NonNull T data, int position, @NonNull Bundle payload){
58-
this.updateItem(data,position);
61+
/**
62+
* if payload is not empty , this method will be call rather than {@link #updateItem(T, int)}
63+
* @param position
64+
*/
65+
public void updatePartWithPayload(int position, @NonNull Bundle payload){
66+
5967
}
6068

6169

diffadapter/src/main/java/com/silencedut/diffadapter/holder/NoDataDifferHolder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.silencedut.diffadapter.holder;
22

3+
import android.support.annotation.NonNull;
34
import android.view.View;
45

56
import com.silencedut.diffadapter.DiffAdapter;
@@ -16,7 +17,7 @@ public NoDataDifferHolder(View itemView, DiffAdapter baseRecyclerAdapter) {
1617
}
1718

1819
@Override
19-
public void updateItem(BaseMutableData data, int position) {
20+
public void updateItem(@NonNull BaseMutableData data, int position) {
2021

2122
}
2223

diffadapter/src/main/java/com/silencedut/diffadapter/utils/AsyncListUpdateDiffer.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ public void run() {
130130
}
131131

132132
private void latchList(@NonNull List<T> newList, @NonNull DiffUtil.DiffResult diffResult) {
133-
this.mList = newList;
134-
this.mReadOnlyList = Collections.unmodifiableList(newList);
133+
updateInnerList(newList);
135134
diffResult.dispatchUpdatesTo(this.mUpdateCallback);
136135
}
137136

@@ -145,6 +144,12 @@ public void updateSingleItem(int changedPosition, Bundle payload) {
145144

146145
}
147146

147+
public void updateInnerList(@NonNull List<T> newList){
148+
++this.mMaxScheduledGeneration;
149+
this.mList = newList;
150+
this.mReadOnlyList = Collections.unmodifiableList(newList);
151+
}
152+
148153

149154
private static class MainThreadExecutor implements Executor {
150155
final Handler mHandler = new Handler(Looper.getMainLooper());

0 commit comments

Comments
 (0)