Skip to content

Commit

Permalink
fix wrong use of softreference
Browse files Browse the repository at this point in the history
  • Loading branch information
李科 committed Aug 10, 2018
1 parent 6c687c9 commit 84fd57c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class NightModelManager {

private boolean modelChanged = false;

private SparseArrayCompat<List<SoftReference<AttrView>>> attrViewMaps = new SparseArrayCompat<>();
private SparseArrayCompat<List<AttrView>> attrViewMaps = new SparseArrayCompat<>();

private static final Map<String, Constructor<? extends View>> sConstructorMap
= new ArrayMap<>();
Expand Down Expand Up @@ -139,11 +139,9 @@ private void applyNewModel() {
modelChanged = true;
int count = attrViewMaps.size();
for (int i=0; i<count; i++) {
List<SoftReference<AttrView>> attrViews = attrViewMaps.valueAt(i);
for (SoftReference<AttrView> attrView : attrViews) {
if (attrView.get() != null) {
attrView.get().apply();
}
List<AttrView> attrViews = attrViewMaps.valueAt(i);
for (AttrView attrView : attrViews) {
attrView.apply();
}
}
}
Expand Down Expand Up @@ -195,13 +193,13 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}

private void putAttrView(AttrView attrView, int hashCode) {
List<SoftReference<AttrView>> attrViews;
List<AttrView> attrViews;
if (attrViewMaps.indexOfKey(hashCode) > -1) {
attrViews = attrViewMaps.get(hashCode);
} else {
attrViews = new ArrayList<>();
}
attrViews.add(new SoftReference<>(attrView));
attrViews.add(attrView);
attrViewMaps.put(hashCode, attrViews);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import android.view.View;

import java.lang.ref.SoftReference;
import java.util.List;

/**
* Created by like on 16/7/20.
*/
public class AttrView {
View view;
SoftReference<View> view;
List<Attr> attrs;

public AttrView(View view, List<Attr> attrs) {
this.view = view;
this.view = new SoftReference<>(view);
this.attrs = attrs;
}

public void apply() {
if (null == view) return;
if (null == view || view.get() == null) return;

for (Attr attr : attrs) {
attr.apply(view);
attr.apply(view.get());
}
}
}

0 comments on commit 84fd57c

Please sign in to comment.