Android Sherlock ActionBar选项卡无法正确分离SupportMapFragment

时间:2013-01-10 00:51:56

标签: android android-actionbar actionbarsherlock actionbarsherlock-map

我有一个带Sherlock ActionBar和支持库的Android项目。 在SherlockFragmentActivity的onCreate中,我使用2个选项卡初始化了ActionBar:

private void configureActionBar(){
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tab = actionBar.newTab()
            .setText("Events")
            .setTabListener(new TabListener<SampleListFragment>(
                    this, "events", SampleListFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setText("Map")
            .setTabListener(new TabListener<SupportMapFragment>(
                    this, "map", SupportMapFragment.class));
    actionBar.addTab(tab);
}

问题是当我回到第一个标签时,屏幕是黑色的,带有灰色的地图控件。如果我在设备屏幕上关闭\,地图将完全删除,并且每一个都很好。

TabListener的实施来自Google手册:

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;

/** Constructor used each time a new tab is created.
 * @param activity  The host Activity, used to instantiate the fragment
 * @param tag  The identifier tag for the fragment
 * @param clz  The fragment's Class, used to instantiate the fragment
 */
public TabListener(Activity activity, String tag, Class<T> clz) {
    mActivity = activity;
    mTag = tag;
    mClass = clz;
}

/* The following are each of the ActionBar.TabListener callbacks */

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    // Check if the fragment is already initialized
    if (mFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
    }
}

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    if (mFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.detach(mFragment);
    }
}

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    // User selected the already selected tab. Usually do nothing.
}

}

1 个答案:

答案 0 :(得分:0)

在使用SlidingMenu实现SupportMapFragment时,我遇到了类似的问题,在搜索论坛后,我发现这是SurfaceView的一个问题,并且可以通过使用此方法设置地图透明来修复它:

private void setMapTransparent(ViewGroup group) {
        int childCount = group.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = group.getChildAt(i);
            if (child instanceof ViewGroup) {
                setMapTransparent((ViewGroup) child);
            } else if (child instanceof SurfaceView) {
                child.setBackgroundColor(0x00000000);
            }
        }
    }

只需在SupportMapFragment的onCreateView上调用该方法。