更改Android的布局片段 [英] Change Android layout with Fragments

查看:255
本文介绍了更改Android的布局片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发的有两个片段一个Android活动。该活动有两种可能的布局,一个在波泰特,另一个景观。

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.

这一个片段是一个 ListFragment ,用的AsyncTask加载从网络列表中的数据。有了这个片段,我没有问题,因为我可以重新创建它。 (虽然这需要一段时间,加载)。

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

其他的片段有一个内部的谷歌地图V2片段:

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>

我创建的的onCreate 方法片段 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();



}

onConfigurationChanged 方法,我有这样的code。正如的onCreate ,我创建了碎片,我改变看法,因为现在我们是在横向/纵向:

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

}

当我在此方法中创建ListFragment的伟大工程,但没有与MapFragment工作。在片段与地图 onCreateView 方法的应用chrash。我觉得我不能再膨胀该片段:

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:

18 04-16:20:32.795:E / AndroidRuntime(15543):android.view.InflateException:二进制XML文件,6号线:错误充气类片段

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

我的问题:

有没有一种方法,以避免碎片的休闲改变布局时?

我怎样才能做到这一点?

谢谢!

推荐答案

有没有办法避免活动的破坏,如果你不使用 onConfigurationChanged 方法的,但这种解决方案是有时难以处理。

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.

净经过研究,我发现了一个很有趣文章谈到如何处理mannualy片段的娱乐,但这种方法不工作我的地图碎片。

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.

我的最终解决方案是让Android的自动改变布局,去掉的android:configChanges =方向从清单

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

另外,我无法使用地图碎片包围谷歌地图V2。我直接用谷歌地图V2片段。

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

要保留片段的实例,你只需要把指令 setRetainInstance(真)

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

感谢您的帮助。

这篇关于更改Android的布局片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆