このサイトのコードは匿名化されていると思います。@Sotirios Delimanolisの回答を提案するつもりでしたが、実際にはやりすぎと少なすぎの両方を行っていると思います。
コントローラーをビューにバインドしすぎており、ビューを相互にバインドしすぎています。なぜそれらすべてを同じswitch
ステートメントに含める必要があるのですか?
別のビューを追加する場合、どの程度変更する必要がありますか?
代わりに、各ビューにPropertyChangeListener
を保持するオブジェクトに を登録させる必要がありますminindex
。ちなみに悪名です。オブジェクトが変更されると、すべてのリスナーminindex
に a を送信する必要があります。PropertyChangeEvent
各ビューのリスナーは、イベントがそのビューを表示することを望んでいるかどうかを確認する必要があります。その場合、ビューはそれ自体を目覚めさせる必要があります。
class ViewController {
private PropertyChangeSupport pcs = new PropertyChangeSupport();
// delegate methods to add and remove listeners to pcs variable.
private int viewIndex; // Changed for documentation. Use String instead?
public void setViewIndex(final int viewIndex) {
int oldIndex = this.viewIndex;
this.viewIndex = viewIndex;
pcs.firePropertyChange("viewIndex", oldIndex, this.viewIndex);
}
}
class ViewOne {
private ViewController vc;
private final Integer myIndex = 1;
// Constructor
public void init() {
// Never add this or an inner class to another object in a constructor.
vc.addPropertyChangeListener("viewIndex",
new PropertyChangeListener() {
public propertyChange(final PropertyChangeEvent evt) {
if (myIndex.equals(evt.getNewValue()) {
setVisibility(View.VISIBLE);
}
}
});
}
}
コンストラクターに関する警告は、コンストラクター内の別のオブジェクトにthis
または内部クラスを公開するthis
と、その外部オブジェクトthis
が完全に構築される前に相互作用する可能性があることです。コンストラクターで PCL をビルドできます。別の方法を使用して、コントローラーに追加します。