0

为了保持向后兼容性,我创建了一个类来访问操作栏:

导入android.app.ActionBar;导入android.app.ActionBar.Tab;导入 android.app.ActionBar.TabListener;导入android.app.FragmentTransaction;

public class ReflectionBar{

    static void getActionBar(Activity a) {

        ActionBar bar = a.getActionBar();

        bar.addTab(bar.newTab().setText("Tab 1").setTabListener(new TabListener() {

                @Override
                public void onTabReselected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTabSelected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }
            }));


    bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowHomeEnabled(true);
    // remove the activity title to make space for tabs
    bar.setDisplayShowTitleEnabled(false);

    return;
    }

}

我在我的 Activity 类中使​​用:

if (android.os.Build.VERSION.SDK_INT>10){
    ReflectionBar bar = new ReflectionBar();
    bar.getActionBar(this);
}

问题是我需要在我的主要活动中收听 onTabSelected 的调用,但我不确定如何设置它。我尝试了一些不同的事情但没有成功,非常感谢任何帮助。

4

1 回答 1

1

问题是我需要在我的主要活动中收听 onTabSelected 的调用,但我不确定如何设置它。我尝试了一些不同的事情但没有成功,非常感谢任何帮助。

Define an interface. Pass an instance of the interface into getActionBar() as a final parameter (which really ought to be named initActionBar(), since you're not returning an action bar, but, that's just me...). In the various onTab... methods in your TabListener objects, call a corresponding method on your interface. You won't be able to pass the ActionBar.Tab object to the interface (since that's API Level 11+), but between the tag and text properties, you should be able to glean something worth passing that identifies the tab to the activity.

于 2011-05-16T18:41:36.020 回答