开发者

Interacting with the actionbar when using reflection

In order to keep backwards compatibility I've created a class to access the action bar:

import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.ActionBar.TabListener; import 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.setD开发者_StackOverflow中文版isplayOptions(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;
    }

}

And I call it in my Activity class using:

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

The problem is that I need to listen to the calls of onTabSelected in my main activity, but I am not sure how to set it up. I've tried a few different things with no success, any help much appreciated.


The problem is that I need to listen to the calls of onTabSelected in my main activity, but I am not sure how to set it up. I've tried a few different things with no success, any help much appreciated.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜