Java 恢复自定义视图

Java 恢复自定义视图,java,android,android-custom-view,Java,Android,Android Custom View,我正在开发一个应用程序,它利用自定义视图MainView.java扩展视图,通过在onClick上设置main活动的视图,使用: public void onClick(View view) { if(view.getId() == R.id.button){ MainView = new MainView(this); setContentView(MainView);

我正在开发一个应用程序,它利用自定义视图MainView.java扩展视图,通过在onClick上设置main活动的视图,使用:

        public void onClick(View view) {
           if(view.getId() == R.id.button){
               MainView = new MainView(this);
               setContentView(MainView);
               MainView.setBackgroundColor(Color.BLACK);
           }
        }
主视图运行游戏,如果玩家输了,我希望屏幕返回显示初始活动_main.xml或任何其他合适的视图。我在MainView的更新方法中检测到丢失

    private void update() {
    if(lives == 0){
        reset();
    }
    if(score >= lvlScore){
        levelUp();
    }
    for(Projectile proj: balls) {//set positions good balls
        proj.setPosition();
    }
    for(Projectile proj: badBalls){//set positions bad balls
        proj.setPosition();
    }

我甚至不知道该怎么做,就是在我的活动中从MainView检索信息,比如分数,以及如何根据MainView中发生的事情将我的自定义视图恢复为初始XML

只需在MainView中引用您的活动即可

在主视图中

public void setParentActivity(final Activity activity){
  this.mActivity = activity;
}
点击更改

    public void onClick(View view) {
       if(view.getId() == R.id.button){
           MainView = new MainView(this);
           setContentView(MainView);
           MainView.setBackgroundColor(Color.BLACK);
           MainView.setParentActivity(this);
       }
    }

使用接口与活动进行通信,就像单击侦听器一样。例如,在MainView类中:

然后,在您的活动中:

// Have your Activity implement the GameEventListener interface
public class MyActivity extends Activity implements GameEventListener {
    public void onClick(View view) {
        if (R.id.button = view.getId()) {
            MainView mainView = new MainView(this);
            mainView.setBackgroundColor(Color.BLACK);

            // Since your Activity implements this interface, you can just
            // set `this` as the listener. Whenever your MainView class
            // calls one of the notify() methods, the implementations below
            // will be triggered.
            mainView.setGameListener(this);
            setContentView(mainView);
        }
    }

    @Override public void onWin() {
        // Reset your view here
    }

    @Override public void onLoss() {
        // Reset your view here
    }
}

这增加了视图和宿主活动之间的紧密耦合。是的,像你建议的那样的界面肯定更好。比我想象的要先进一点,因为沃利自己都搞不懂。。。
// Have your Activity implement the GameEventListener interface
public class MyActivity extends Activity implements GameEventListener {
    public void onClick(View view) {
        if (R.id.button = view.getId()) {
            MainView mainView = new MainView(this);
            mainView.setBackgroundColor(Color.BLACK);

            // Since your Activity implements this interface, you can just
            // set `this` as the listener. Whenever your MainView class
            // calls one of the notify() methods, the implementations below
            // will be triggered.
            mainView.setGameListener(this);
            setContentView(mainView);
        }
    }

    @Override public void onWin() {
        // Reset your view here
    }

    @Override public void onLoss() {
        // Reset your view here
    }
}