Fragment嵌套高德地图,切换黑屏,切换卡死退出解决方案

http://blog.csdn.net/u010378579/article/details/52797641

 

布局:
– 首页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>

Leave a Reply

Your email address will not be published. Required fields are marked *