我在父类中有一个函数createTabs
,用于创建应用程序栏标签并将tabListener
附加到它们,并在tabListener的onTabSelected
中检查所选标签并通过意图调用其活动
现在在所有活动类中,我在onCreate
方法中调用此函数,以实例化该活动的选项卡。
当我在以下语句中添加标签时,问题出现在createTabs
中:
actionBar.addTab(actionBar.newTab().setText("Event Feed")
.setIcon(R.drawable.ic_action_alarms)
.setTabListener(tabListener),0,false);
它自动调用onTabSelected
侦听器方法,当将控件传递给我反对在onCreate
方法中调用此函数的activity类时,为了实例化选项卡,这会在无限循环中发送我的应用程序。
onTabSelected
之后调用.addTab
?请帮助:
父活动类中的createTabs方法: public void createTabs(ActionBar actionBar){
try {
// final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
int tabPosition = tab.getPosition();
switch (tabPosition) {
case 0:
Intent startFeedActivity = new Intent(
MainActivity.this, EventFeed.class);
startActivity(startFeedActivity);
break;
case 1: {
Intent startWideActivity = new Intent(MainActivity.this,
WideView.class);
startActivity(startWideActivity);
break;
}
case 2:
setContentView(R.layout.event_feed);
break;
}
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
};
actionBar.addTab(actionBar.newTab().setText("Event Feed")
.setIcon(R.drawable.ic_action_alarms)
.setTabListener(tabListener),0,false);
actionBar.addTab(actionBar.newTab().setText("Wide")
.setIcon(R.drawable.ic_action_Wide)
.setTabListener(tabListener),1,true);
actionBar.addTab(actionBar.newTab().setText("Report")
.setIcon(R.drawable.ic_action_location_found)
.setTabListener(tabListener),2,false);
} catch (Exception e) {
Log.getStackTraceString(e.getCause().getCause());
setContentView(R.layout.activity_main);
Toast.makeText(this, "Oops Batman!", Toast.LENGTH_SHORT).show();
}
}
在Activity类中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_feed);
createTabs(getActionBar());
loadFeed();
}
在调试器中,可以看到ActionBarImpl.selectTab
之后调用了ActionBarImpl.addTab
MainActivity$1.onTabSelected(ActionBar$Tab, FragmentTransaction) line: 44
ActionBarImpl.selectTab(ActionBar$Tab) line: 572
ActionBarImpl.addTab(ActionBar$Tab, int, boolean) line: 511
WideView(MainActivity). createTabs (ActionBar) line: 74
WideView.onCreate(Bundle) line: 20
WideView(Activity).performCreate(Bundle) line: 5133
答案 0 :(得分:1)
<击> 你需要做的是在添加标签后设置监听器:
ActionBar.Tab wideTab = actionBar.newTab().setText("Wide")
.setIcon(R.drawable.ic_action_Wide);
actionBar.addTab(wideTab,1,true);
wideTab.setTabListener(tabListener);
击> <击> 撞击>
你是对的;这不起作用。如果你在活动中复制这段代码(你不应该 - 它应该放在'基础'活动中,然后你的其他人应该扩展它),只需从你的听众onTabSelected
方法中拯救出适当的案例
作为旁注,我建议您使用Fragments作为标签内容,而非活动。
class MyTabListener implements ActionBar.TabListener {
private int mTabPosition;
public MyTabListener(int tabPosition) {
mTabPosition = tabPosition;
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
int tabPosition = tab.getPosition();
// If we're trying to select this already-selected tab, bail out.
if (tabPosition == mTabPosition) return;
switch (tabPosition) {
case 0:
Intent startFeedActivity = new Intent(
MainActivity.this, EventFeed.class);
startActivity(startFeedActivity);
break;
case 1: {
Intent startWideActivity = new Intent(MainActivity.this,
WideView.class);
startActivity(startWideActivity);
break;
}
case 2:
setContentView(R.layout.event_feed);
break;
}
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
然后在添加标签时:
actionBar.addTab(actionBar.newTab().setText("Event Feed")
.setIcon(R.drawable.ic_action_alarms)
.setTabListener(new MyTabListener(0)),0,false);