创建ActionBar选项卡也调用其selectTab函数

时间:2013-09-30 00:40:49

标签: java android

我在父类中有一个函数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类时,为了实例化选项卡,这会在无限循环中发送我的应用程序。

  1. 我的问题是为什么在onTabSelected之后调用.addTab
  2. 这个无限循环的解决方案是什么?
  3. 请帮助:

    父活动类中的

    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 
    

1 个答案:

答案 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);