Android:滚动后 RecyclerView 内容混乱 [英] Android: RecyclerView content messed up after scrolling

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

问题描述

我使用 RecyclerView 来显示一个标记列表,值的每个标记都显示为一个 CardView.但是在向下滚动 RecyclerView 并向后滚动后,卡片的某些内容丢失了,如下面的两个屏幕截图所示.滚动后红色矩形内的内容丢失.

I'm using RecyclerView to display a list of marks, and each mark of the value is shown as a CardView. But some contents of the cards are lost after the scrolling the RecyclerView down and scrolling back, as shown in the two screenshots below. The contents in the red rectangle is lost after scrolling.

在滚动之前;

滚动之后;

我在想是不是 RecyclerView 的 bug,google 了一下也没找到解决办法.

I'm wondering whether or not it's a bug of RecyclerView and find no solution after Googling for it.

除标题外,所有视图都是不可见的,它们的可见性取决于标记的值.

All views are invisible except the title, their visibilities are depends on the mark's value.

有谁知道为什么会发生这种情况?

Does anyone know why this would happen?

推荐答案

onBindHolder() 被多次调用,因为回收器需要一个视图,除非在视图类型改变时有一个新的视图.因此,每次您在子视图中设置可见性时,其他视图的状态也会发生变化.

onBindHolder() is called several times as recycler needs a view, unless there's a new one when view type is changed. So each time you set visibility in child views, other views states are also changing.

每当您上下滚动时,这些视图都会以错误的可见性选项重新绘制.

Whenever you scroll up and down, these views are getting re-drawn with wrong visibility options.

解决方案:

您有一个 setValue 方法检查值并设置为查看.如果需要,它调用另一个方法showView".您需要实现 else 语句(值为 0 或 null)并在那里隐藏视图...

You have a setValue method check values and set to view. If neccessary it calls another method "showView". You need to implement else statement (which is value is 0 or null) and hideView there...

void setValue(Object value, TextView textView, TableRow row, View seperator) {
    if (value != null) {
        if (!isEmpty(value.toString())) {
            textView.setText(String.valueOf(value));
            showViews(row, seperator);
        }
    } else
        hideViews(row, seperator);
}

private void showViews(TableRow row, View seperator) {
    row.setVisibility(View.VISIBLE);
    seperator.setVisibility(View.VISIBLE);
}

private void hideViews(TableRow row, View seperator) {
    row.setVisibility(View.INVISIBLE); // if there is a empty space change it with View.GONE
    seperator.setVisibility(View.INVISIBLE);
}

这篇关于Android:滚动后 RecyclerView 内容混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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