观察者.更新();
上述方法使观察者的可见性“消失”,但我想更改除发送者观察者之外的所有其他观察者。我该如何控制这个?
我的所有动作都是观察者并在他们的构造函数中注册,如下所示,
public class ParentAction extends AbstractAction implements IActionObserver{
private ArrayList<IAction> lSubItems;
private View subView;
public ParentAction( String ItemText,int drawable,ArrayList<IAction> SubItems) {
super(ItemText,drawable);
lSubItems = SubItems;
ActionHolder.getInstance().registerObserver(this);
}
@Override
public void update() {
getSubView().setVisibility(View.GONE);
} ...
行动持有人
public class ActionHolder implements IActionSubject {
private static ActionHolder uniqueActionHolder;
private ArrayList observers;
private ActionHolder() {
observers = new ArrayList();
}
public static synchronized ActionHolder getInstance() {
if (uniqueActionHolder == null) {
uniqueActionHolder = new ActionHolder();
}
return uniqueActionHolder;
}
public void registerObserver(IActionObserver o) {
observers.add(o);
}
public void removeObserver(IActionObserver o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
public void notifyObserver() {
for (int i = 0; i < observers.size(); i++) {
IActionObserver observer = (IActionObserver) observers.get(i);
observer.update();
}
}
public void actionClicked(View view) {
notifyObserver();
}
}