Swisyn Swisyn - 1 year ago 277
Android Question

Android: RecyclerView content messed up after scrolling

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.

enter image description here

enter image description here

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

Here is the layout file of cardview; http://codepaste.net/rpk7sc
All views are invisible except the title, their visibilities are depends on the mark's value.

Here is the RecyclerView adapter; http://codepaste.net/2389ke

Answer Source

onBindHolder called several times as recycler news needs a view unless new one. 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.

Solution :

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())) {
            showViews(row, seperator);
    } else
        hideViews(row, seperator);

private void showViews(TableRow row, View seperator) {

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