开发者社区> 问答> 正文

RecyclerView适配器ViewHolder ID有时为-1

我正在使用Recyclerview显示图像和其他内容的列表。当recyclerview加载并快速滚动时,它崩溃了。

当我调试时,我可以看到

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
...
viewHolder.getItemId() // this returns -1, sometimes

我怀疑没有完全创建Recyclerview Adapter视图,对此有什么建议吗?

展开
收起
Puppet 2019-12-03 16:22:36 600 0
1 条回答
写回答
取消 提交回答
  • 由于您尚未共享代码,我们不确定您的情况如何,但让我分享一个可能与您有关的情况。RecyclerView崩溃的可能情况:

    1. This crash might be caused by the fact that the setViewHolder and onBind 
    methods are being called while your list(used by the adapter) is being updated, but notifyDataSetChanged() hasn't been called.
    
    2. This problem may caused when android start dealing with bitmaps. the main 
    reason is lack of memory space and also, garbage collector cannot free some 
    space.
    
    3. If you using image , please confirm the image size because the size may 
    cause this problem. Some case recyclerview was not able to hold the images 
    with big resolution.
    
    4. Double check if you are using all the data loading process in ( main/UI 
    thread ), it my testing Looper.getMainLooper() == Looper.myLooper().
    
    2019-12-03 16:23:01
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载