Skip to content Skip to sidebar Skip to footer

Why Do Values ​disappear After Scrolling In Recycler View?

Data before scrolling Data after scrolling The problem with my app is shown in the pictures above. After entering the data, if i scroll after adding the item as scrollable, the

Solution 1:

The issue lies with your TextWatcher that you're adding in onBindViewHolder.

At the moment you have it setup so that every time the RecyclerView binds a view (which can happen multiple times per actual view), you're adding a new TextWatcher and then also setting the text to the item's weight, which is then triggering the previous watchers you added, setting the item's weight to something else, in this case an empty string.

What you should be doing is either removing all listeners before you add another one, or add the listener in onCreateViewHolder and use the holder's adapter position to get your item.


Here is some pseudocode to clarify my suggestions:

Adding the listener in onCreateViewHolder

RoutineDetailViewHolder {
    private EditText weight;
    RoutineDetailViewHolder {
        weight.addTextChangedListener {
            
            items[adapterPosition].setWeight(...)
        }
    }
}

Removing the listeners before binding again:

RoutineDetailViewHolder {
    private EditText weight;
    private TextWatcher weightWatcher;

    voidbind() {
    
        weight.removeTextChangedListener(weightWatcher);
        
        weightWatcher = new TextWatcher();
        weight.addOnTextChangedListener(weightWatcher);
    }
}

Post a Comment for "Why Do Values ​disappear After Scrolling In Recycler View?"