Skip to content

Commit

Permalink
Fixed #568 - Possible memory leak when using Sticky Headers
Browse files Browse the repository at this point in the history
Fixed #575 - Sticky header is disappearing when scrolling
  • Loading branch information
davideas committed Mar 22, 2018
1 parent 8f310f7 commit 044efaf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Methods and Size](https://img.shields.io/badge/Methods%20and%20size-core:%201099%20|%20119%20KB-e91e63.svg)](http://www.methodscount.com/?lib=eu.davidea%3Aflexible-adapter%3A5.0.0%2B)

# FlexibleAdapter
- [v5.0.2](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.2) built on 2018.03.17
- [v5.0.3](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.3) built on 2018.03.23
- If you come from previous versions, update your code following the Wiki page [Migrations](https://github.com/davideas/FlexibleAdapter/wiki/Migrations).
- Please read also [issues](https://github.com/davideas/FlexibleAdapter/issues) and [releases](https://github.com/davideas/FlexibleAdapter/releases).

Expand Down Expand Up @@ -58,7 +58,7 @@ repositories {
```
dependencies {
// Using JCenter
compile 'eu.davidea:flexible-adapter:5.0.2'
compile 'eu.davidea:flexible-adapter:5.0.3'
compile 'eu.davidea:flexible-adapter-ui:1.0.0-b3'
compile 'eu.davidea:flexible-adapter-livedata:1.0.0-b2'
compile 'eu.davidea:flexible-adapter-databinding:1.0.0-b2'
Expand All @@ -70,7 +70,7 @@ dependencies {
#### Stay Updated
|Flexible Adapter|Live Data|Data Binding|UI|
|---|---|---|---|
|<div align="center">5.0.2</div>|<div align="center">1.0.0-b2</div>|<div align="center">1.0.0-b2</div>|<div align="center">1.0.0-b3</div>
|<div align="center">5.0.3</div>|<div align="center">1.0.0-b2</div>|<div align="center">1.0.0-b2</div>|<div align="center">1.0.0-b3</div>
|<a href='https://bintray.com/davideas/maven/flexible-adapter?source=watch' alt='Get automatic notifications about new "flexible-adapter" versions'><img src='https://www.bintray.com/docs/images/bintray_badge_color.png'></a>|<a href='https://bintray.com/davideas/maven/flexible-adapter-livedata?source=watch' alt='Get automatic notifications about new "flexible-adapter-livedata" versions'><img src='https://www.bintray.com/docs/images/bintray_badge_bw.png'></a>|<a href='https://bintray.com/davideas/maven/flexible-adapter-databinding?source=watch' alt='Get automatic notifications about new "flexible-adapter-databinding" versions'><img src='https://www.bintray.com/docs/images/bintray_badge_bw.png'></a>|<a href='https://bintray.com/davideas/maven/flexible-adapter-ui?source=watch' alt='Get automatic notifications about new "flexible-adapter-ui" versions'><img src='https://www.bintray.com/docs/images/bintray_badge_bw.png'></a>

# Wiki!
Expand Down Expand Up @@ -117,9 +117,10 @@ This [Wiki page](https://github.com/davideas/FlexibleAdapter/wiki/5.x-%7C-Demo-A

# Change Log
###### Latest release
[v5.0.2](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.2) - 2018.03.17
[v5.0.3](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.3) - 2018.03.23

###### Old releases
[v5.0.2](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.2) - 2018.03.17
[v5.0.1](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.1) - 2018.03.11 |
[v5.0.0](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.0) - 2018.03.04<br>
[v5.0.0-rc4](https://github.com/davideas/FlexibleAdapter/releases/tag/5.0.0-rc4) - 2017.12.17 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -597,13 +596,14 @@ public void updateDataSet(@Nullable List<T> items) {
* @see #setAnimateToLimit(int)
* @see #onPostUpdate()
* @since 5.0.0-b7 Created
* <br>5.0.0-b8 Synchronization animations limit
* <br>5.0.0-rc2 Copy of the Original List done internally
*/
@CallSuper
public void updateDataSet(@Nullable List<T> items, boolean animate) {
mOriginalList = null; // Reset original list from filter
if (items == null) items = new ArrayList<>();
// Always clear cache of bound view holders
discardBoundViewHolders();
if (animate) {
mHandler.removeMessages(UPDATE);
mHandler.sendMessage(Message.obtain(mHandler, UPDATE, items));
Expand Down Expand Up @@ -1769,10 +1769,11 @@ public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position,
item.bindViewHolder(this, holder, position, payloads);
// Avoid to show the double background in case header has transparency
// The visibility will be restored when header is reset in StickyHeaderHelper
if (areHeadersSticky() && !isFastScroll && mStickyHeaderHelper.getStickyPosition() >= 0 && payloads.isEmpty()) {
if (areHeadersSticky() && isHeader(item) && !isFastScroll && mStickyHeaderHelper.getStickyPosition() >= 0 && payloads.isEmpty()) {
int headerPos = getFlexibleLayoutManager().findFirstVisibleItemPosition() - 1;
if (headerPos == position && isHeader(item))
if (headerPos == position) {
holder.itemView.setVisibility(View.INVISIBLE);
}
}
}
// Endless Scroll
Expand Down Expand Up @@ -4236,7 +4237,7 @@ public FlexibleAdapter setDiffUtilCallback(DiffUtilCallback diffUtilCallback) {

private synchronized void animateDiff(@Nullable List<T> newItems, Payload payloadChange) {
if (useDiffUtil) {
Log.v(TAG, "Animate changes with DiffUtils! oldSize=" + getItemCount() + " newSize=" + newItems.size());
log.v("Animate changes with DiffUtils! oldSize=" + getItemCount() + " newSize=" + newItems.size());
if (diffUtilCallback == null) {
diffUtilCallback = new DiffUtilCallback();
}
Expand Down Expand Up @@ -4397,7 +4398,7 @@ private void applyAndAnimateMovedItems(List<T> from, List<T> newItems) {

private synchronized void executeNotifications(Payload payloadChange) {
if (diffResult != null) {
Log.i(TAG, "Dispatching notifications");
log.i("Dispatching notifications");
mItems = diffUtilCallback.getNewItems();// Update mItems in the UI Thread
diffResult.dispatchUpdatesTo(this);
diffResult = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ public void onViewRecycled(RecyclerView.ViewHolder holder) {
}
}

/**
* To call when views are all discarded.
*/
void discardBoundViewHolders() {
mBoundViewHolders.clear();
}

/**
* Usually {@code RecyclerView} binds 3 items more than the visible items.
*
Expand Down Expand Up @@ -555,6 +562,7 @@ public Set<Integer> getSelectedPositionsAsSet() {
* @since 1.0.0
*/
public void onSaveInstanceState(Bundle outState) {
discardBoundViewHolders();
outState.putIntegerArrayList(TAG, new ArrayList<>(mSelectedPositions));
if (getSelectedItemCount() > 0) log.d("Saving selection %s", mSelectedPositions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,13 @@ private void translateHeader() {
private void swapHeader(FlexibleViewHolder newHeader, int oldHeaderPosition) {
if (mStickyHeaderViewHolder != null) {
resetHeader(mStickyHeaderViewHolder);
// #568, #575 - Header ViewHolder out of the top screen must be recycled manually
if (mHeaderPosition > oldHeaderPosition) {
mAdapter.onViewRecycled(mStickyHeaderViewHolder);
}
}
mStickyHeaderViewHolder = newHeader;
// mStickyHeaderViewHolder.setIsRecyclable(false);
mStickyHeaderViewHolder.setIsRecyclable(false);
ensureHeaderParent();
onStickyHeaderChange(mHeaderPosition, oldHeaderPosition);
}
Expand Down Expand Up @@ -300,7 +304,7 @@ private void resetHeader(FlexibleViewHolder header) {
if (!header.itemView.equals(view)) {
addViewToParent(((ViewGroup) header.itemView), view);
}
//header.setIsRecyclable(true);
header.setIsRecyclable(true);
// #294 - Expandable header is not resized / redrawn on automatic configuration change when sticky headers are enabled
header.itemView.getLayoutParams().width = view.getLayoutParams().width;
header.itemView.getLayoutParams().height = view.getLayoutParams().height;
Expand Down

0 comments on commit 044efaf

Please sign in to comment.