1. 程式人生 > >Fragment巢狀高德地圖,切換黑屏,切換卡死退出解決方案

Fragment巢狀高德地圖,切換黑屏,切換卡死退出解決方案

佈局:
- 首頁Activity使用FragmentTabHost切換3個Fragment,Fragment1中巢狀2個Fragment,一個Fragment裝了高德地圖MapView,還有一個普通的Fragment;

問題1:
- 在首頁從地圖Fragment切換到別的Fragment時出現短暫黑屏

解決方案:
- 佈局中用TextureMapView代替MapView,其他基本都一樣。
- 裝有地圖的Fragment繼承TextureSupportMapFragment(這個加不加好像沒什麼影響)

問題2:
- 在首頁從地圖Fragment切換到別的Fragment再切回來出現卡死退出

解決方案:
- 首頁的Fragment不用FragmentTabHost切換,因為每次切換Fragment都會onCreateView,佔用太多記憶體,導致奔潰;
- 在首頁第一次載入所有的Fragment,切換的時候通過hide、show的方式來管理Fragment,這樣就不會每次切換都呼叫onCreateView方法了;

參考程式碼:

private List<Fragment> fragmentList;

    private static final int INDEX_HOME_FRAGMENT = 0;
    private static final
int INDEX_ORDER_FRAGMENT = 1; private static final int INDEX_MY_FRAGMENT = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); AppManager.getAppManager().addActivity(this); initUi(); initFragments(); } private
void initFragments() { if (fragmentList == null) { fragmentList = new ArrayList<>(); fragmentList.add(new HomeFragment()); fragmentList.add(new OrderFragment()); fragmentList.add(new MyFragment()); if (fragmentList.size() == 3) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); for (int i = 0; i < fragmentList.size(); i++) { Fragment fragment = fragmentList.get(i); transaction.add(R.id.fl_home_fragment_container, fragment); transaction.hide(fragment); } transaction.show(fragmentList.get(INDEX_HOME_FRAGMENT)); transaction.commit(); } } } protected void initUi() { RadioGroup homeTabs = (RadioGroup) findViewById(R.id.rg_home_tabs); homeTabs.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int checkFragment = INDEX_HOME_FRAGMENT; switch (checkedId) { /*首頁*/ case R.id.rb_home_tab_home: checkFragment = INDEX_HOME_FRAGMENT; break; /*訂單*/ case R.id.rb_home_tab_order: checkFragment = INDEX_ORDER_FRAGMENT; break; /*我的*/ case R.id.rb_home_tab_me: checkFragment = INDEX_MY_FRAGMENT; break; default: break; } //切換Fragment FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); for (int i = 0; i < fragmentList.size(); i++) { if (checkFragment == i) { transaction.show(fragmentList.get(i)); } else { transaction.hide(fragmentList.get(i)); } } transaction.commit(); }

相應佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.onetoo.www.onetoo.activity.HomeActivity"
    >

    <FrameLayout
        android:id="@+id/fl_home_fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        />
    <View
        android:id="@+id/divide"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/activity_group_radioGroup"
        android:background="@color/shenhui"/>

    <RadioGroup
        android:id="@+id/rg_home_tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <RadioButton
            android:id="@+id/rb_home_tab_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:button="@null"
            android:checked="true"
            android:drawableTop="@drawable/selector_home_btn"
            android:gravity="center"
            android:padding="6dp"
            android:text="首頁"
            android:textColor="@color/selector_color_tab"
            android:textSize="@dimen/font_size_middle"
            />

        <RadioButton
            android:id="@+id/rb_home_tab_order"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:button="@null"
            android:drawableTop="@drawable/selector_chat_btn"
            android:gravity="center"
            android:text="訂單"
            android:textColor="@color/selector_color_tab"
            android:textSize="@dimen/font_size_middle"
            />

        <RadioButton
            android:id="@+id/rb_home_tab_me"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:button="@null"
            android:drawableTop="@drawable/selector_my_btn"
            android:gravity="center"
            android:text="我的"
            android:textColor="@color/selector_color_tab"
            android:textSize="@dimen/font_size_middle"
            />
    </RadioGroup>
</LinearLayout>