Skip to content

Commit

Permalink
Merge pull request #12 from romasku/master
Browse files Browse the repository at this point in the history
Fix race condition during header view replacement
  • Loading branch information
lizapopova authored Apr 28, 2021
2 parents 895c7df + 1a304e0 commit c0f1471
Showing 1 changed file with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,18 @@ public void run() {
headerView.requestLayout();
}
});
addView(headerView);
removePrevHeaderView();
// Uses postponed runnable, because removeViewAt(int) should not be invoked from drawing
// related methods (e.g. {@link #onScrollListener} is invoked from RecyclerView's
// onLayout(boolean, int, int, int, int)).
post(new Runnable() {
@Override
public void run() {
if (getChildCount() > 1) {
removeViewAt(1);
}
addView(headerView);
}
});
}

/* Uses postponed runnable, because removeViewAt(int) should not be invoked from drawing
Expand Down Expand Up @@ -205,24 +215,6 @@ public void run() {
});
}

/**
* Removes previous header view if exists. Uses postponed runnable, because removeViewAt(int)
* should not be invoked from drawing related methods (e.g. {@link #onScrollListener} is invoked
* from RecyclerView's onLayout(boolean, int, int, int, int)).
*/
private void removePrevHeaderView() {
if (getChildCount() > 2) {
post(new Runnable() {
@Override
public void run() {
if (getChildCount() > 2) {
removeViewAt(1);
}
}
});
}
}

/**
* Calculates yTranslation for the current header view based on its height and next header
* position.
Expand Down

0 comments on commit c0f1471

Please sign in to comment.