删除列表中的最后一项时,RecyclerView错误

时间:2017-08-05 14:14:18

标签: android android-recyclerview

我的recyclerview有问题。我有删除列表中的项目的功能。 当最后一项没有显示时,它工作正常。但是当最后一项显示在屏幕(列表)的末尾时,我从列表中删除项目后出现错误。

这是我的logcat:

java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true
at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5778)
at android.support.v7.widget.RecyclerView$Recycler.quickRecycleScrapView(RecyclerView.java:5885)
at android.support.v7.widget.RecyclerView$LayoutManager.removeAndRecycleScrapInt(RecyclerView.java:8547)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3698)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3365)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3886)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15664)
at android.view.ViewGroup.layout(ViewGroup.java:4869)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2246)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

我删除了这样的项目:

public void delete(Entity entity) {
      entities.remove(entity);
      notifyDataSetChanged();
}

这里有什么问题?对我的问题有任何建议吗?谢谢!

我想可能是这个错误的原因是删除项目我从Dispoable的rx线程调用?当我在活动中删除项目时,它的工作正常。

1 个答案:

答案 0 :(得分:0)

您可以使用$httpProvider.responseInterceptors.push("YourInterceptor"); Picker.route('/generate/getPdf', function(params, req, res, next) { var doc = new PDFDocument({size: 'A4', margin: 50}); doc.fontSize(12); doc.text('PDFKit is simple', 10, 30, {align: 'center', width: 200}); res.writeHead(200, { 'Content-Type': 'application/pdf', 'Content-Disposition': 'attachment; filename=test.pdf' }); doc.pipe(res); doc.end(res); }); 方法notifyItemRangeChanged适配器。