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

查看:96
本文介绍了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的错误,并且在谷歌搜索之后找不到解决方案.

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 called several times as recycler needs a view unless new one in case of view type changed. So each time you set visilibity in child views, other views states are also changes.

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

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

解决方案:

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

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天全站免登陆