RecyclerView混合项目 [英] RecyclerView Mixing Up Items

查看:54
本文介绍了RecyclerView混合项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,它显示通过RecyclerView适配器从Internet下载的图像和文本的列表.当我上下滚动RecyclerView时会出现问题.当我最初向下滚动列表时,一切似乎都很好,所有CardView(包含图像和一些文本的Cardview)都处于各自的位置.

I have a RecyclerView that displays a list of images and text that are downloaded from the internet via a RecyclerView adapter. The problem arises when I scroll up and down the RecyclerView. As I scroll down the list initially, everything seems fine and all the CardViews (a cardview containing an image and some text) are in their respective positions.

但是,当我开始向上滚动时,所有以前的CardView都被最低的CardView取代.recyclerview的最低项目被重用以替换RecyclerView中的所有先前项目.

However, as I begin to scroll back up, all the previous CardViews are replaced by the lowest CardView. The lowest item of the recyclerview is reused to replace all the previous items in the RecyclerView.

这是RecyclerView错误吗?如果不是,则可以通过清除RecyclerView的缓存或强制RecyclerView回收正确的物品来解决此问题?

Is this a RecyclerView bug? If not is there some way to solve this issue by maybe clearing the RecyclerView's cache or by forcing RecyclerView to recycle the correct items?

推荐答案

这实际上是我的适配器实现中的问题.我在重写的onBindViewHolder方法中有一个游标,每次它调用 cursor.moveToNext()时,除非它是数据库的最后一行,否则游标会向前移动.到那时,它将停止在最后一行.因此,当我向上滚动屏幕时,recyclerview可以正常工作,只是现在它向我显示了光标给出的最后一行中的相同数据.我以为最初可能是recyclerview错误,但事实证明,这是我代码中的错误.为了解决这个问题,我只是将 cursor.moveToNext()更改为 cursor.moveToPosition(position),它开始按预期显示结果.

It was actually a problem in my adapter implementation. I had a Cursor in the overriden onBindViewHolder method and every time it called cursor.moveToNext() the cursor would move forward unless it was the last row in the database. At that point it would stop on that last row. So when I scrolled up the screen, the recyclerview worked perfectly, except now it showed me the same data from the last row that the cursor had given it. I thought this might have been a recyclerview bug initially, but as it turns out, this was a bug in my code. To solve it I simply changed cursor.moveToNext() to cursor.moveToPosition(position) and it started displaying results as expected.

这篇关于RecyclerView混合项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆