20180301_W4
。。。 | 復(fù)現(xiàn)
ava.lang.IndexOutOfBoundsException
Inconsistency detected. Invalid view holder adapter positionViewHolder
validateViewHolderForOffsetPosition
—— 第一輪
代碼結(jié)構(gòu)
1. 有沒有可能是網(wǎng)絡(luò)操作慢,一張照片,一張照片添加集合,然后在 updataset 晚了
2. 通過預(yù)先定義一部分?jǐn)?shù)據(jù),然后 debug 模式慢慢加,會不會崩潰?
3. adapterUpload.remove(photoEntity.getIndex()); 上傳錯誤,?
4. 數(shù)組超索引,應(yīng)該主要是數(shù)據(jù)刪除引起的 | ? 刪除的寫法沒有問題
5. 多個線程同時上傳失敗,刪除圖片?
6. 網(wǎng)絡(luò)堵塞下嘗試操作?
初始化控件,賦值,點(diǎn)擊事件
初始化控件
賦值
賦值 lst view data
請求網(wǎng)絡(luò) | lst view data
請求網(wǎng)絡(luò)后
點(diǎn)擊事件
拍錄 或 看視頻
拍完照片,看完后上傳網(wǎng)絡(luò)
網(wǎng)絡(luò)上傳后
3 篇 Stack Overflow 文章
i once got the error too:
I had a same issue previously. Finally found a workaround for that
this problem may happen
??In my case I was trying to?change my adapter contents on a background thread
Just remove all views of your layout Manager before notify?
In my case, I was updating the items and calling?notifyDataSetChanged?in a non-UI thread
?It also can be related with setting the adapter multiple times at the same time.
Use notifyDataSetChanged()instead
?I had a similar problem but not exactly the same
In my case (delete/insert data in my data structure) I needed to clear recycle pool and then notify data set changed!————mRecyclerView.getRecycledViewPool().clear();————mAdapter.notifyDataSetChanged();?
—— 第二輪(生命周期推斷操作? | recyclerview 方法 | 百度谷歌查找)
百度谷歌查找 10
此問題是由RecyclerView不同線程中修改的數(shù)據(jù)引起的。最好的方法是檢查所有數(shù)據(jù)訪問。解決方法正在打包LinearLayoutManager。
所以建議你,如果不是必須要用這個控件的話,就換成 listview/gridview 吧,畢竟是已經(jīng)很成熟的控件,出問題的概率小很多。
如果必須要用到的話,建議你盡量減少 notifyxxx()這個方法的使用。如果不得不用,那么必須保證是在主線程中進(jìn)行調(diào)用。
來進(jìn)行排查,發(fā)現(xiàn)了問題所在:從服務(wù)器獲取到刪除圖片成功的返回值后,先進(jìn)行了 data 的清除,然后在主線程里面又結(jié)合 notifyItemRemoved 重新刪除了一次!
我也報了同樣的錯,解決方法:集合數(shù)據(jù)一旦發(fā)生改變,立馬得調(diào)用 notifyDataSetChanged();,我就是因?yàn)樵僬{(diào)用 list.clear(), 方法后沒有及時調(diào)用 notifyDataSetChanged() 方法,導(dǎo)致的,現(xiàn)在已經(jīng)解決,希望能給犯同樣錯誤的小伙伴帶來幫助
回復(fù) RxWorld:我去,還真的是這樣子,我把 datalist 放在子線程進(jìn)行計(jì)算修改,然后切換到 主線程去 notify,結(jié)果就異常。沒有立馬 notify 引出的異常。目前用樓主的 方法 catch 住了。搞了我一條。
博主的方法可能有效,但我試了,還是不行。然后看了別的文章,懂了一個道理,list 的數(shù)據(jù),要和 adpater 數(shù)據(jù)同步,例如,list.clear(), 執(zhí)行后,要立即調(diào)用 notifyDataSetChanged(),而不是等到網(wǎng)絡(luò)請求完后,在調(diào)用 notifyDataSetChanged()。意思是每當(dāng) list 集合改變時,就立馬調(diào)用通知。這樣就沒報錯了。希望對你們有幫助。
回復(fù)天耀 106:是不是可以理解為: 刷新數(shù)據(jù)是先 adapter 中的 list 清空并 notifyDataSetChanged, 而不是等 list 賦新值后 notifyDataSetChanged
回復(fù)天耀 106:有個問題啊 如果在 list.clear 后就直接調(diào)用適配器刷新 會白屏一下,這樣體驗(yàn)是不是很不好。看別的 app 如小紅書之類的就沒有白屏 是有其他解決方法么
生命周期推斷操作
finish 調(diào)用 onDestroy