Change Android layout with Fragments

怎甘沉沦 提交于 2019-12-24 01:53:15

问题


I am developing an Android Activity which has two Fragments. The Activity has two possible layouts, one in Portait, and another one in Landscape.

I have created the Fragments working well, but I'm having troubles when I change the orientation of the Phone/tablet.

One of this Fragment is a ListFragment, with an AsyncTask to load the list data from the web. With this Fragment I don't have problems, because I can create it again. (Although it takes a while loading).

The other Fragment has a Google Map V2 fragment inside:

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

I create the fragments from the onCreate method of the FragmentActivity:

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



}

In the onConfigurationChanged method, I have this code. As in onCreate, I create the Fragments, and I change the view because now we are on Landscape/Portrait:

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

}

When I create in this method the ListFragment works great, but is not working with the MapFragment. The application chrash in the onCreateView method of the Fragment with a Map. I think I can't inflate again this fragment:

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

My questions:

Is there a way to avoid the recreation of the fragments when changing the layout?

How can I do this?

Thanks!


回答1:


There is no way to avoid the destruction of the Activity if you don't use the onConfigurationChanged method, but this solution is sometimes is difficult to handle.

After researching on net, I found a very interesting article talking about how to handle the recreation of the fragments mannualy, but this solution doesn't work to my Map Fragment.

My final solution is to let Android change the layout automatically, removing android:configChanges=”orientation” from the manifest.

Also, I can't use the Map Fragment enclosing the Google Map V2. I directly used the Google Map V2 fragment.

To retain the instance of the Fragment, you only have to put the instruction 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();
}

Thanks for your help.




回答2:


If all you need is a different layout for portrait and landscape, there's an easier way to do this. Create a folder layout-land and put the layout XML's in there for any fragments/activities you want to specify a different layout in landscape. Android will take care of which layout to use depending on the orientation. Check out this page on developer.android.com for more info.

Here's another example of what I mean.



来源:https://stackoverflow.com/questions/16042510/change-android-layout-with-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!