RecyclerView异步设置holder时插入新项崩溃

发布于 2022-09-04 08:50:49 字数 4872 浏览 18 评论 0

viewholder有一个textview和imageview
textview显示用户姓名
imageview显示用户头像

在onbindviewholder时会根据item的id向服务器请求该用户的信息
由于请求是异步的,当请求成功回调设置textview和imageview的时候列表插入新信息,就会报错

错误信息如下:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{a2d62b7 position=2 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
    at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5046)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5177)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3330)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3186)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1799)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
    at android.view.View.layout(View.java:15718)
    at android.view.ViewGroup.layout(ViewGroup.java:5039)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2129)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1886)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1103)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5949)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
    at android.view.Choreographer.doCallbacks(Choreographer.java:601)
    at android.view.Choreographer.doFrame(Choreographer.java:571)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5308)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲜肉鲜肉永远不皱 2022-09-11 08:50:49

报错信息不足, 无法判断是什么原因造成的.

所有的图片异步加载框架都不会考虑下面这个问题:

当前ViewHolder是否仍是请求前的ViewHolder

不论是AdapterView还是RecyclerView都有视图重用的机制, 重用的视图肯定是ItemViewType相同的视图, 既然ItemViewType相同, 那么用户代码的处理逻辑应当就是相同的.

如果自己实现一套图片异步加载框架的话, 可以参考Android官网文档: Processing Bitmaps Off the UI Thread
另外, 不建议占用View的tag属性, 因为你有可能需要使用tag作为它用.

魔法唧唧 2022-09-11 08:50:49

你报错是因为在非ui线程更新界面吧...

为了防止异步加载出现图片错乱,可以给view加入tag,在异步任务完成后根据tag决定是否加载。

极致的悲 2022-09-11 08:50:49

这种应该是先判断本地是否有缓存,没有则向服务器获取。获取完毕后更新adapter对应的数据,更新列表即可。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文