简体   繁体   中英

Android Sherlock ActionBar Tab can't detach SupportMapFragment properly

I have an Android project with Sherlock ActionBar and Support Library. In the onCreate of SherlockFragmentActivity I initialized ActionBar with 2 tabs:

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);
}

The problem is when I go back to the first tab, the screen is black with grayed Map controls. If I turn off\\on the device screen, the Map completely removes and everyting is fine.

TabListener implementation is from the Google Manual:

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.
}

}

I came across with similar issue once when implementing the SupportMapFragment with a SlidingMenu, after searching forums I found that it was an issue with surfaceView and that it could be fixed by setting the map transparent with this method:

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);
            }
        }
    }

Just call that method on your SupportMapFragment's onCreateView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM