开发中发现app内wishlist页面中心愿单商品,两个及以上时,同时取消关注,刷新列表会导致页面Crash。log如下:
08-24 16:09:28.138 14091 14091 E AndroidRuntime: Process: com.cider, PID: 14091 08-24 16:09:28.138 14091 14091 E AndroidRuntime: java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true com.cider.widget.LoadMoreRecycleView{d65fece VFED..... ......ID 13,0-707,1183 #7f09024c app:id/rvProductList}, adapter:com.cider.widget.LoadMoreRecycleView$WrapAdapter@96471ef, layout:androidx.recyclerview.widget.StaggeredGridLayoutManager@85584fc, context:com.cider.ui.activity.main.MainActivity@d3aac0d 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:6433) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$Recycler.quickRecycleScrapView(RecyclerView.java:6554) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleScrapInt(RecyclerView.java:9249) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4207) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3862) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at com.scwang.smartrefresh.layout.SmartRefreshLayout.onLayout(SmartRefreshLayout.java:617) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1565) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1565) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at com.cider.widget.NoPreloadViewPager.onLayout(NoPreloadViewPager.java:911) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1565) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at com.cider.widget.swipebackhelper.SwipeBackLayout.onLayout(SwipeBackLayout.java:278) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at com.android.internal.policy.DecorView.onLayout(DecorView.java:757) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.View.layout(View.java:20699) 08-24 16:09:28.138 14091 14091 E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6198) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2865) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2392) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1527) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7394) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1036) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:832) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:767) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1022) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.os.Looper.loop(Looper.java:201) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6810) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 08-24 16:09:28.139 14091 14091 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)w
问题分析:Recyclerview更新数据时报crash
解决方案:在清空list后及时通知adapter更新下数据,即调用adapter的notifyDataSetChanged方法。之后的逻辑获取到数据list后再notifyDataSetChanged,即可正常展示数据