Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AppBarStateChangeListener 引起的内存泄漏 #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public enum Type {OVERLAP, FOLLOW, DRAG, SCROLL}
private RecyclerView.OnScrollListener onRecyclerScrollListener;
private NestedScrollView.OnScrollChangeListener onNestedScrollChangeListener;
private OnScrollChangeListener onScrollChangeListener;
private AppBarStateChangeListener appBarStateChangeListener;

@Override
protected void onAttachedToWindow() {
Expand All @@ -112,12 +113,27 @@ protected void onAttachedToWindow() {
AppBarLayout appBarLayout = SpringHelper.findAppBarLayout(this);
appBarCouldScroll = SpringHelper.couldScroll(appBarLayout);
if (appBarLayout != null) {
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
appbarState = state;
}
});
if (appBarStateChangeListener == null) {
appBarStateChangeListener = new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
appbarState = state;
}
};
}
appBarLayout.addOnOffsetChangedListener(appBarStateChangeListener);
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
//解决ViewPager更新内容时出现的内存泄漏
if (appBarStateChangeListener != null) {
AppBarLayout appBarLayout = SpringHelper.findAppBarLayout(this);
if (appBarLayout != null) {
appBarLayout.removeOnOffsetChangedListener(appBarStateChangeListener);
}
}
}

Expand Down