RecyclerView更改错误值的上滚动向上/向下 [英] RecyclerView change wrong value's on scroll up / down

查看:124
本文介绍了RecyclerView更改错误值的上滚动向上/向下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

previously我用一个ListView和为ListView自定义适配器。
所有工作正常,除非我向上和向下滚动的数据发生改变(如我的if语句我的适配器以更改ListView项的背景色。当我向上和向下滚动的颜色改变错误的,在第一次加载并滚动所有被罚款!)

previously i used a listview and a custom adapter for the listview. all worked fine, except when i scroll up and down the data changed (e.g i've an if statement i the adapter to change the bg color of the listview item. when i scroll up and down the color changed wrong, on the first load and scroll all was fine!)

我通过在getView方法在我的适配器始终重置视图为null解决了这个问题列表视图。

i solved this problem for the listview by resetting the view always to null in the getView method in my adapter.

v = null;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.friendlv, null);

现在我切换到RecyclerView有我同样的问题,但我不知道解决这个问题。

now i switched to RecyclerView and there i've the same issue but i've no idea to solve this issue..

谢谢!

推荐答案

在您设置的属性,这是该项目的持续性。滚动时,这一观点从屏幕上消失,并且同一个项目布局再次用于下一个项目。所以,你需要写else条件重置旧属性。

Once you set the property, it is persistent for that item. When you scroll, that view disappears from the screen and that same item layout is used again for next item. So you need to write else condition to reset the old properties.

所以,你应该添加其他语句如下面的例子说明你所有的如果语句

So, you should add else statement to all your if statements as illustrated in following example

if (condition) {
    change bg color to new color
}
else {
    change bg color to old color
}

这篇关于RecyclerView更改错误值的上滚动向上/向下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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