开发者

OnClickListener not firing from Parent class

开发者 https://www.devze.com 2023-01-11 16:29 出处:网络
A Follow up to this question: Group of Views (controls) on multiple screens I have created a parent class and a child class that inherits from it.When I set the OnClickListener in the child class, th

A Follow up to this question: Group of Views (controls) on multiple screens

I have created a parent class and a child class that inherits from it. When I set the OnClickListener in the child class, the event fires when the button is clicked. When I move the set OnClickListener to the parent class, the event doesn't fire. I've got to be missing something obvious but I just don't see it.

Thanks, Cameron.

Parent Class:

public class NavigationMenu extends Activity 
{
    @Override
    protected void onCreate开发者_StackOverflow(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nav_bar);

        Button loginButton = (Button) findViewById(R.id.navbtnHome);
        loginButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                Toast.makeText(getApplicationContext(), "Cameron, Im here", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(NavigationMenu.this, Login.class);
                startActivity(i);
            }
        });
    }
}

Child Class:

public class Settings extends NavigationMenu 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
    }
}


The problem is that the setContentView of your Settings class will reset the layout that was created by your parent class' onCreate(). And therefore, the callbacks as well.

To work around that, I would suggest that you add a LinearLayout with id "container" in your nav_bar layout.

You then leave the NavigationMenu class untouched and in your child class, you just insert your view in the container. This should work :

  public class Settings extends NavigationMenu 
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            LinearLayout container = (LinearLayout)findViewById(R.id.container);
            View settingsView = getLayoutInflater().inflate(R.layout.settings, null);
            container.addView(settingsView);
        }
    }

Now, a cleaner way to do that would be to force your NavigationMenu's child class to provide something to be put in the container.

You could achieve that by adding an abstract method to NavigationMenu that would require the children to create the view.

public abstract class NavigationMenu extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nav_bar);

        Button loginButton = (Button) findViewById(R.id.navbtnHome);
        loginButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                Toast.makeText(getApplicationContext(), "Cameron, Im here", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(NavigationMenu.this, Login.class);
                startActivity(i);
            }
        });

        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        container.addView(createView());
    }

    protected abstract View createView();
}

And in your child class, you just have to implement createView () :

  public class Settings extends NavigationMenu 
    {
        @Override
        protected View createView()
        {
            LinearLayout container = (LinearLayout)findViewById(R.id.container);
            return getLayoutInflater().inflate(R.layout.settings, null);
        }
    }

This has the advantage that if you change your layout (rename your container for example), you would have to modify only the parent class.


You should call super.onCreate after setContentView in your child but you have to work around because your parent calls setcontentview with another view

0

精彩评论

暂无评论...
验证码 换一张
取 消