android - 使用 Fragment 更改 Android 布局

标签 android android-fragments google-maps-android-api-2 onconfigurationchanged

我正在开发一个有两个 fragment 的 Android Activity。该 Activity 有两种可能的布局,一种为纵向,另一种为横向。

我已经创建了运行良好的 fragment ,但是当我更改手机/平板电脑的方向时遇到了麻烦。

其中一个 Fragment 是一个 ListFragment,它带有一个 AsyncTask 从网络加载列表数据。有了这个 fragment 我就没有问题了,因为我可以再次创建它。 (尽管加载需要一段时间)。

另一个 fragment 内部有一个Google Map V2 fragment :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/mapInicio"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

我通过 FragmentActivityonCreate 方法创建 fragment :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_inicio);



    Fragment listFragment = new ListaEstaciones();
    FragmentTransaction transList = getSupportFragmentManager()
            .beginTransaction();
    transList.add(R.id.listaestaciones_f_container, listFragment);
    transList.commit();



    Fragment mapFragment = new MapaGoogleV2();
    FragmentTransaction transMap = getSupportFragmentManager()
            .beginTransaction();
    transMap.add(R.id.mapa_f_container, mapFragment);
    transMap.commit();



}

onConfigurationChanged方法中,我有这段代码。正如在 onCreate 中一样,我创建了 fragment ,并更改了 View ,因为现在我们处于横向/纵向:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    setContentView(R.layout.activity_inicio);

    Fragment listFragment = new ListaEstaciones();
    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
    transaction.add(R.id.listaestaciones_f_container, listFragment);
    transaction.commit();



    Fragment mapFragment = new MapaGoogleV2();
    FragmentTransaction transMap = getSupportFragmentManager()
            .beginTransaction();
    transMap.add(R.id.mapa_f_container, mapFragment);
    transMap.commit();

}

当我用这种方法创建时,ListFragment 工作得很好,但不能与 MapFragment 一起使用。 Fragment with a MaponCreateView 方法中的应用程序崩溃。我想我不能再次膨胀这个 fragment :

04-16 18:20:32.795: E/AndroidRuntime(15543): android.view.InflateException: Binary XML file line #6: Error inflating class fragment

我的问题:

有没有办法在更改布局时避免重新创建 fragment ?

我该怎么做?

谢谢!

最佳答案

如果不使用onConfigurationChanged方法,就无法避免Activity的销毁,但这种解决方案有时很难处理。

在网上研究后,我发现了一个very interesting article谈论如何手动处理 fragment 的重新创建,但此解决方案不适用于我的 map fragment 。

我的最终解决方案是让 Android 自动更改布局,从 list 中删除 android:configChanges=”orientation”

此外,我无法使用包含 Google map V2 的 map fragment 。我直接使用了Google Map V2 fragment 。

要保留Fragment的实例,只需输入指令setRetainInstance(true):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_inicio);

    Fragment listFragment = new ListaEstaciones();
    listFragment.setRetainInstance(true);
    FragmentTransaction transList = getSupportFragmentManager()
        .beginTransaction();
    transList.add(R.id.listaestaciones_f_container, listFragment);
    transList.commit();
}

感谢您的帮助。

关于android - 使用 Fragment 更改 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16042510/

相关文章:

android - 如何在没有 Android 工具栏的 Fragment 中视差滚动布局到另一个布局

android - Google Maps Android API v2 抛出 GooglePlayServicesNotAvailableException,过期,SupportMapFragment.getMap() 返回 null

Android 和谷歌地图 - 使用后退按钮关闭信息窗口

android - ListView 的 onListItemClick 不起作用 - Android

线程中的android应用程序类

android - 从右向左滑动过渡动画。 fragment -> Activity

android - which apk runs on the Android device ionic

java - 应用程序存储设置的 Intent

java - 为什么我的 fragment 无法显示来自 recyclerview 的数据?