If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.
The hierarchy of views in a tab implementation is:
The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));
You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);
In essence, you are adding your OnClickListener
to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener
will need to do that for you.
Here is a full example, which lets you intercept the click event, and change the content below the tab:
final String myTabTag = "My Tab";
final int myTabIndex = 3;
getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );
getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
getTabHost().setCurrentTab(myTabIndex );
}
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…