BilalMH BilalMH - 2 months ago 54
Android Question

RecyclerView not clearing (notifyDataSetChanged not working)

I've had a look everywhere on here and nothing is related to my problem. Basically I've implemented a feature which changes the reading direction of my app (1 or -1) now I can get it to initially change direction and it works really well but when I get it to change back (using the same code but changing the direction) it just doesn't update. Just seems like notifyDataSetChanged(); doesn't want to work the second time...?

My code is as as follows:

private void flip() {

if (!isFlipped) {
mData.getItems().removeAll(mData.getItems());
mAdapter.notifyDataSetChanged();
isFlipped = true;
loadData(false, -1);
closeMenu();
} else {

mData.getItems().removeAll(mData.getItems());
mAdapter.notifyDataSetChanged();
isFlipped = false;
loadData(false, 1);
closeMenu();
}

}


What am I doing wrong?

Again, thanks in advance :)

Answer

Implement a public method in your RecyclerView code, e.g:

public void clearAll(){
    mData.clear();
    this.notifyDataSetChanged();
}

And then call that function from your activity (or Fragment):

private void flip() {

if (!isFlipped) {
    mAdapter.clearAll();
    isFlipped = true;
    loadData(false, -1);
    closeMenu();
} else {
    mAdapter.clearAll();
    isFlipped = false;
    loadData(false, 1);
    closeMenu();
}

}