From 76f7f94c1145d37bad9b7c14079ab0f23493e125 Mon Sep 17 00:00:00 2001 From: jason Date: Sun, 27 Sep 2020 17:57:14 +0800 Subject: [PATCH 1/2] Fix Memory leaks because of AppBarStateChangeListener --- .../springview/widget/SpringView.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java b/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java index 1e2f5f0..230e0b8 100644 --- a/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java +++ b/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java @@ -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() { @@ -112,16 +113,31 @@ 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); + } } } - public SpringView(Context context) { + public SpringView( Context context) { this(context, null); } From 3c4cd8d152ac991e03fc8ed638c3aa5fe56f0197 Mon Sep 17 00:00:00 2001 From: jason Date: Sun, 27 Sep 2020 19:31:16 +0800 Subject: [PATCH 2/2] Format code --- .../main/java/com/liaoinstan/springview/widget/SpringView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java b/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java index 230e0b8..c9d56f2 100644 --- a/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java +++ b/library/src/main/java/com/liaoinstan/springview/widget/SpringView.java @@ -137,7 +137,7 @@ protected void onDetachedFromWindow() { } } - public SpringView( Context context) { + public SpringView(Context context) { this(context, null); }