【问题标题】:Android Sherlock ActionBar Tab can't detach SupportMapFragment properlyAndroid Sherlock ActionBar Tab 无法正确分离 SupportMapFragment
【发布时间】:2013-01-15 03:41:36
【问题描述】:

我有一个带有 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.
}

}

【问题讨论】:

    标签: android android-actionbar actionbarsherlock actionbarsherlock-map


    【解决方案1】:

    我在使用 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 上调用该方法即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多