搜索recyclerview崩溃,国内所有技术文章逃不了要链接到stackoverflow的RecyclerView crashes when “scrapped or attached views may not be recycled”,它的解决方案是设置recyclerview的属性android:animateLayoutChanges为false(如果您需要代码设置那就是调用recyclerview.setLayoutTransition(null);)。然并卵在我集成recyclerview到react-native中这么设置依然崩溃。
我在开发中遇到两个最多次的崩溃问题是
- IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true
- IllegalStateException: Cannot call this method unless RecyclerView is computing a layout or scrolling
快速滚动崩溃在stackoverflow和google官方论坛都有人反馈RecyclerView crashes on scrolling、 Issue 226353: RecyclerView crash when scrolling fast
实际并没有很好的解决方案,只能减少崩溃的情况并不意味彻底解决RecyclerView的崩溃。可见RecyclerView坑还不少,还需要一段时间来完善。以下是我摸索出的一些方法:
- 设置android:animateLayoutChanges=false(或者recyclerview.setLayoutTransition(null););
- setNestedScrollingEnabled(false)可以缓解快速滚动崩溃;
- setFitsSystemWindows(true);
- 切记不要重写ItemAnimator一定要使用DefaultItemAnimator;
- 重写LinearLayoutManager的supportsPredictiveItemAnimations 返回false;
- DefaultItemAnimator setSupportsChangeAnimations(false);
希望对您有帮助!