java - Reverting a custom view -
java - Reverting a custom view -
i working on app utilizes custom view,mainview.java extends view, setting view mainactivity on onclick using:
public void onclick(view view) { if(view.getid() == r.id.button){ mainview = new mainview(this); setcontentview(mainview); mainview.setbackgroundcolor(color.black); } }
the mainview runs game , if player "loses" want screen homecoming showing initial activity_main.xml, or other suitable view. observe loss in mainview's update() method.
private void update() { if(lives == 0){ reset(); } if(score >= lvlscore){ levelup(); } for(projectile proj: balls) {//set positions balls proj.setposition(); } for(projectile proj: badballs){//set positions bad balls proj.setposition(); }
what have been unable figure out retrieve info mainview in activity, score, , how revert custom view initial xml based on happens in mainview.
use interface communicate activity, click listener. example, in mainview
class:
// maintain reference listener should notified of events private gameeventlistener mlistener; // interface defining events listener receive public interface gameeventlistener { void onwin(); void onloss(); } public void setgameeventlistener(gameeventlistener listener) { mlistener = listener; } private void notifygamewon() { if (mlistener != null) { mlistener.onwin(); } } private void notifygamelost() { if (mlistener != null) { mlistener.onloss(); } }
then, in activity:
// have activity implement 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 activity implements interface, can // set `this` listener. whenever mainview class // calls 1 of notify() methods, implementations below // triggered. mainview.setgamelistener(this); setcontentview(mainview); } } @override public void onwin() { // reset view here } @override public void onloss() { // reset view here } }
java android android-custom-view
Comments
Post a Comment