Skip to content

Commit 9a44a62

Browse files
committed
Use Observable to notify.
1 parent d61f838 commit 9a44a62

4 files changed

Lines changed: 46 additions & 14 deletions

File tree

activitytaskview/src/main/java/cc/rome753/activitytask/model/ViewPool.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import android.view.ViewGroup;
66

77
import java.util.LinkedList;
8+
import java.util.Observable;
89

910
import cc.rome753.activitytask.view.ATextView;
1011

11-
public class ViewPool {
12+
public class ViewPool extends Observable {
1213

1314
LinkedList<ATextView> pool = new LinkedList<>();
1415
private static ViewPool factory = new ViewPool();
@@ -21,6 +22,8 @@ public void recycle(ViewGroup viewGroup) {
2122
for(int i = 0; i < viewGroup.getChildCount(); i++) {
2223
View view = viewGroup.getChildAt(i);
2324
if(view instanceof ATextView) {
25+
removeParent(view);
26+
view.setTag(null);
2427
pool.add((ATextView) view);
2528
} else if(view instanceof ViewGroup) {
2629
recycle((ViewGroup) view);
@@ -30,12 +33,12 @@ public void recycle(ViewGroup viewGroup) {
3033
}
3134

3235
public ATextView getOne(Context context) {
33-
ATextView view;
36+
ATextView view;notifyObservers();
3437
if(pool.isEmpty()) {
3538
view = new ATextView(context);
39+
addObserver(view);
3640
} else {
3741
view = pool.remove();
38-
removeParent(view);
3942
}
4043
return view;
4144
}
@@ -46,4 +49,8 @@ private void removeParent(View view) {
4649
}
4750
}
4851

52+
public void notifyLifecycleChange(LifecycleInfo info) {
53+
setChanged();
54+
notifyObservers(info);
55+
}
4956
}

activitytaskview/src/main/java/cc/rome753/activitytask/view/ATextView.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
import android.graphics.Color;
55
import android.text.SpannableString;
66
import android.text.Spanned;
7+
import android.text.TextUtils;
78
import android.text.style.AbsoluteSizeSpan;
89
import android.util.AttributeSet;
910

1011
import androidx.appcompat.widget.AppCompatTextView;
1112

13+
import java.util.Observable;
14+
import java.util.Observer;
15+
16+
import cc.rome753.activitytask.model.LifecycleInfo;
17+
1218
/**
1319
* change text color when update
1420
* Created by rome753@163.com on 2017/4/3.
1521
*/
1622

17-
public class ATextView extends AppCompatTextView{
23+
public class ATextView extends AppCompatTextView implements Observer {
1824

1925
private static AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(8, true);
2026

@@ -36,20 +42,39 @@ public void setInfoText(String s, String lifecycle) {
3642
int i1 = s.indexOf("@");
3743
String tag = s.substring(i1);
3844
setTag(tag);
45+
s = s.replace("Activity", "A…");
46+
s = s.replace("Fragment", "F…");
47+
s = s.replace(tag, " ");
48+
49+
addLifecycle(s, lifecycle);
50+
}
3951

52+
private void addLifecycle(String s, String lifecycle) {
4053
lifecycle = lifecycle.replace("onFragment", "");
4154
lifecycle = lifecycle.replace("onActivity", "");
4255
lifecycle = lifecycle.replace("SaveInstanceState", "SIS");
43-
s = s.replace("Activity", "A…");
44-
s = s.replace("Fragment", "F…");
45-
s = s.replace(tag, " ") + lifecycle;
4656

47-
int i2 = s.indexOf(" ");
48-
49-
setTextColor(s.contains("Resume") ? Color.YELLOW : Color.WHITE);
50-
SpannableString span = new SpannableString(s);
51-
span.setSpan(absoluteSizeSpan, i2, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
57+
setTextColor(lifecycle.contains("Resume") ? Color.YELLOW : Color.WHITE);
58+
SpannableString span = new SpannableString(s + lifecycle);
59+
span.setSpan(absoluteSizeSpan, s.length(), span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
5260
setText(span);
5361
}
5462

63+
@Override
64+
public void update(Observable o, Object arg) {
65+
if(getTag() == null) {
66+
return;
67+
}
68+
if(arg instanceof LifecycleInfo) {
69+
LifecycleInfo info = (LifecycleInfo) arg;
70+
String s = info.fragments != null ? info.fragments.get(0) : info.activity;
71+
int i1 = s.indexOf("@");
72+
String tag = s.substring(i1);
73+
if(TextUtils.equals((CharSequence) getTag(), tag)) {
74+
s = getText().toString();
75+
s = s.substring(0, s.lastIndexOf(" ") + 1);
76+
addLifecycle(s, info.lifecycle);
77+
}
78+
}
79+
}
5580
}

activitytaskview/src/main/java/cc/rome753/activitytask/view/ActivityTaskView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void remove(LifecycleInfo info) {
139139

140140
public void update(LifecycleInfo info) {
141141
aTree.updateLifecycle(info.activity, info.lifecycle);
142-
notifyData();
142+
ViewPool.get().notifyLifecycleChange(info);
143143
}
144144

145145
private void notifyData() {

activitytaskview/src/main/java/cc/rome753/activitytask/view/FragmentTaskView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void remove(List<String> fragments) {
4545

4646
public void update(LifecycleInfo info) {
4747
mTree.updateLifecycle(info.fragments.get(0), info.lifecycle);
48-
notifyData();
48+
ViewPool.get().notifyLifecycleChange(info);
4949
}
5050

5151
private void notifyData(){

0 commit comments

Comments
 (0)