网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
android.view.ViewGroup.layout(ViewGroup.java:5108) at
android.widget.FrameLayout.layoutChildren(FrameLayout.java:633) at
android.widget.FrameLayout.onLayout(FrameLayout.java:568) at
android.view.View.layout(View.java:15912) at
android.view.ViewGroup.layout(ViewGroup.java:5108) at
android.widget.FrameLayout.layoutChildren(FrameLayout.java:633) at
android.widget.FrameLayout.onLayout(FrameLayout.java:568) at
android.view.View.layout(View.java:15912) at
android.view.ViewGroup.layout(ViewGroup.java:5108) at
android.widget.FrameLayout.layoutChildren(FrameLayout.java:633) at
android.widget.FrameLayout.onLayout(FrameLayout.java:568) at
android.view.View.layout(View.java:15912) at
android.view.ViewGroup.layout(ViewGroup.java:5108) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813) at
android.widget.Line`
数组越界?NoNoNo。整篇日志,均没有涉及到我们的代码,一时让人也摸不着头脑,经过一番查阅资料才发现,论坛上早已炸锅。该 bug 竟然是 Google 程序员的锅?
从 StackOverFlow 相关资料得知,该 Bug 主要是由于 Adapter 绑定的集合和 RecyclerView 的数据不一致而导致。
直接说解决思路
找到了症结,问题解决解决起来也是非常简单,且听我细细到来。
直接采用 notifyDataSetChanged
同步外部数据集和内部数据集。[ 不是很推荐 ]
use notifyDataSetChanged() will avoid this crash, but it will kill Animation and Performance.
该方法比较简单,但失去了动画效果,而且更新数据的性能较低。另外,如果对外部数据集做了两次以上的操作,却只调用 `notifyDataSetChanged` 同步一次,也极有可能报上述错误。
直接 Try 住这个 Bug【最简单粗暴】
直接复写 LinearLauoutManager。
`package com.zxedu.ischool.common;import android.content.Context;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.AttributeSet;/**
- Author: nanchen
- Email: liushilin520@foxmail.com
- Date: 2017-05-19 15:56
*/public class WrapContentLinearLayoutManager extends LinearLayoutManager {
public WrapContentLinearLayoutManager(Context context) {
super(context);
}
public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}`
对,没错,直接更换 LayoutManaer 就 OK 了
// mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // 解决RecyclerView可能出现的holder数组越界Bug mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
顺藤摸瓜,做最优雅的程序员
既然我们知道了崩溃发送原因,我们可以直接顺藤摸瓜。在进行数据移除和数据增加的时候,保证 RecyclerView 的 Adapter 中的数据集移除和添加等操作后的数据集保持一致。
RecyclerView 内部的数据集这里我们不妨叫「内部数据集」,而我们传递给 Adapter 的,姑且叫它 「外部数据集」。我们通过「外部数据集」更新「内部数据集」,一般会使用到以下的方法。
-
notifyItemRangeRemoved()
-
notifyItemRangeInserted()
-
notifyItemRangeChanged()
-
notifyDataSetChanged()
我们一般不采用 `notifyDataSetChanged()` 方法,因为它不但没有默认的动画效果,而且在更新数据的效率上会大打折扣,官方并不推荐。
Don’t call notifyDataSetChanged if you don’t have to.
我们一般在给 Adapter 设置数据时会这样做。
public void setData(List<Data> data){ if(data != null){ mData.clear(); mData.addAll(data); notifyItemRangeChanged(0,mData.size()); } }
实际上这段代码,并无毛病,但假设 data 数据为接口返回的数据,刷新后返回的数据和现在 Adapter 中的数据数目不一致的时候,极有可能出现开题的 bug,直接崩溃。所以我们不妨可以这样修复。
`public void setData(List data){ if(data != null){ int size = mData.size();
mData.clear();
notifyItemRangeRemoved(0,size);
mData.addAll(data);
notifyItemRangeInserted(0,mData.size());
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
.csdn.net/topics/618658159)**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!