1

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方法中调用此函数的活动类时,实例化选项卡,这会将我的应用程序发送到无限循环中。

  1. 我的问题是为什么onTabSelected会被调用.addTab
  2. 摆脱这个无限循环的解决方案是什么?

请帮忙:

父活动类中的 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();

    }

}

在活动类中:

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 
4

1 回答 1

1

您需要做的是在添加选项卡后设置监听器:

ActionBar.Tab wideTab = actionBar.newTab().setText("Wide")
            .setIcon(R.drawable.ic_action_Wide);
actionBar.addTab(wideTab,1,true);
wideTab.setTabListener(tabListener);

你是对的; 那是行不通的。如果您在活动之间复制此代码(您不应该 - 它应该进入“基础”活动,然后您的其他人应该扩展它),只需从侦听器onTabSelected方法中排除适当的情况即可。

作为旁注,我建议您考虑将片段用于您的标签内容,而不是活动。

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);
于 2013-09-30T01:34:10.250 回答