Fragment with RecyclerView : java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true

后端 未结 2 1308
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 07:33

I keep getting java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true when using Fragment with Re

2条回答
  •  长发绾君心
    2021-01-19 08:00

    Ok so I've found were the problem was. In my ViewHolder I had in addition to the layout Views, an ORMlite entity (could've been any object that was not part of the layout). The problem was that the ViewHolder's equals() and hashcode() methods were based on the entity which was null. The reason it was null was because with RecyclerView you don't have access to the data position in onCreateViewHolder(), but only in onBindViewHolder(). The adapter data is a list of ORMlite entities in my case, and I want to bundle the entity inside the holder. (Still have to find a way to do this...)

    I was expecting a NullPointerException in this case. I've manage to get a NullPointerException by calling holder.setIsRecyclable(true).

    Hope this helps others in need..

    Thanks

提交回复
热议问题