简体   繁体   English

Android ActionBar Tabs onclick问题

[英]Android ActionBar Tabs onclick issue

i have a problem with the onclick event for tabs at the ActionBar. 我在ActionBar上的标签的onclick事件有问题。 I want to "outsource" the ActionBar-Tab logic to a class, so that i can reuse the ActionBar-Tabs-navigation in all activities. 我想将ActionBar-Tab逻辑“外包”到一个类,这样我就可以在所有活动中重用ActionBar-Tabs-navigation。

Here is the outsourced "ActionBar-Tab" logic: 这是外包的“ActionBar-Tab”逻辑:

public class TabActivity extends Activity implements TabListener 

{ {

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab);

    // Set up the ActionBar to show tabs:
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Add Tabs:
    actionBar.addTab(actionBar.newTab().setText("Dashboard").setTabListener(this),0,true);
    actionBar.addTab(actionBar.newTab().setText("Search").setTabListener(this),1,false);
    actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(this),2,false);
}

@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) 
{
    switch(arg0.getPosition())
    {
       case 0:
           Intent dashboard = new Intent(this,DashBoardActivity.class);
           startActivity(dashboard);
       break;

       case 1:
           Intent suche = new Intent(this,SucheActivity.class);
           startActivity(suche);
       break;

       case 2:
           // Start Intent
       break;

       case 3:
           // Start Intent
       break;

       case 4:
         // Start Intent
       break;
    }
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {

}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {

}

} }

And here is the DashBoardActivity which extends the TabActivity class: 这是扩展TabActivity类的DashBoardActivity:

public class DashBoardActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dash_board);
}

} }

The DashBoardActivity is also the Launcher Activity for my app. DashBoardActivity也是我的应用程序的启动器活动。

So, my problem. 所以,我的问题。 When the App starts, the ActionBar navigation is successfully show as expected. 应用程序启动时,ActionBar导航将按预期成功显示。 I saw (via Log.i), that when the App starts, the ActionBar's onTabSelected() is called (without clicking it). 我看到(通过Log.i),当应用程序启动时,ActionBar的onTabSelected()被调用(不点击它)。

As you can see, i want to start different Activities depending on the Tab that was clicked. 如您所见,我想根据单击的选项卡启动不同的活动。

The problem: The app starts,- onTabSelected is called automatically on launch,- and then the Activity "DashBoardActivity.class" get started. 问题:应用程序启动, - 启动时自动调用onTabSelected,然后启动活动“DashBoardActivity.class”。 (but the current activity is DashBoardActivity!?!) (但目前的活动是DashBoardActivity!?!)

The started Activity extends the TabActivity,- but the ActionBar-Tabs are not show. 已启动的Activity扩展了TabActivity,但ActionBar-Tabs未显示。

Is there a solution ? 有解决方案吗?

Change this line: 改变这一行:

actionBar.addTab(actionBar.newTab().setText("Dashboard").setTabListener(this),0,true);

to: 至:

actionBar.addTab(actionBar.newTab().setText("Dashboard").setTabListener(this),0,false);

What I got from the Android Documentation: 我从Android文档获得的内容:

public abstract void addTab (ActionBar.Tab tab, int position, boolean setSelected) public abstract void addTab(ActionBar.Tab tab,int position,boolean setSelected)

  • setSelected - True if the added tab should become the selected tab. setSelected - 如果添加的选项卡应成为选定的选项卡,则为True。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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