简体   繁体   中英

“Header” Views and buttons: how do I attach listeners to Buttons in a “header” that does not have its own Activity?

I have touched on this question here , where Christopher gave an answer to this, but I don't really understand it, so I thought it's time to make it a real question, not just a "follow up" =)


As it stands, the application I'm writing has 4 different screens:

  1. Screen 1 - list of nodes (main screen)
  2. Screen 2 - options menu, tableLayout with buttons
  3. Screen 3 - navigation
  4. Screen 4 - text details on version etc

These screens can be navigated to/from using a "header" View that is placed on top. The header then has 4 different buttons:

+--------------------+
| menu with buttons  |
+--------------------+
|                    |
|                    |
|                    |
|  C O N T E N T     |
|                    |
|                    |
|                    |
+--------------------+

The header is just an XML-file (header.xml) with a few buttons. That header.xml is the included in the Layouts using the include-markup. For example, the main.xml has the line:

<include layout="@layout/header"></include>

The header show up alright, but the question is - what is the correct approach to attach OnClickListeners for the buttons in the header?

Christopher pointed out that you could create an Activity class and do the hooks there, like this:

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);

        // This needs to be done *after* the View has been inflated
        setupHeaderButtons();
    }
}

First, I can't make it work since the method setupHeaderButtons isn't accessible from FirstActivity. Secondly, is this the right way to go at it?

The setupHeaderButtons() method is protected so it can only be accessed by classes that extend that base class, which is BaseActivity in this case.

Are you sure that your FirstActivity is extending BaseActivity ?

I would prefer this so you don't have to remember (and probably forget) to invoke setupHeaderButtons for every derived Activity. BTW, set U pHeaderButtons it's a better name.

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);
            setupHeaderButtons();
    }

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);
    }
}

I personally don't think you should overcomplicate things. Having to call setupHeaderButtons should be fine, especially if you only have a handful of activities.

If you're using standard launch modes, the activity will be relaunched. Check out Application Fundamentals if you're interested in learning about launch modes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM