无法用另一个片段替换制表符内的片段

时间:2014-10-14 12:23:30

标签: android android-layout android-fragments google-maps-android-api-2 mapfragment

我正在使用Android应用,其中有4个标签位于操作栏(main_activity.xml)。

在一个标签中,我将其称为MapListFragment(map_list_fragment.xml),我必须显示两个不同的片段(一些数据的列表和地图视图表示)。所有其他选项卡都是片段,并与MapListFragment(id / main_fragments_container)放在同一个容器中。

因此,首先显示地图片段,并在底部显示按钮地图和列表。当时只显示一个片段(地图或列表),在按钮单击(地图或列表)上,您可以显示其他片段,然后再次单击按钮返回到第一个片段。这些map和list Fragment的容器是(id / fragments_container)。我通过调用FragmentTransaction上的replace()方法来更改此Fragment,并转发ActionBar TabListener用于更改选项卡的相同容器(id / main_fragments_container)。我还尝试为特别是这个选项卡创建新容器,并将其用于此选项卡中的所有操作,但我遇到了同样的问题。更改此片段效果很好,但是......

然后我在ActionBar上添加了一个按钮,它调用了新的Fragment,它应该代表添加一些数据的视图。这个片段的容器是(id / main_fragments_container)问题是,当我回到MapListFragment时,我无法获得与初始相同的行为。第一个问题是这个最好的工作实践还是应该调用Activity而不是Fragment?

当我从ActionBar按钮调用new Fragment时,我总是删除地图片段(我得到相同的结果,当我不删除时),当我回到MainFragment时,显示空白视图,虽然我调用了addToBackStack()方法。

所以我在这里遇到了很多问题,我希望我能解释其中的一些问题,有谁能知道我能尝试做些什么呢?

包含4个标签的MainActivity的XML布局。

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_gravity="center"
     android:orientation="vertical" 
     android:background="@android:color/white">

     <LinearLayout
         android:id="@+id/main_fragments_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >
     </LinearLayout>

 </LinearLayout>

MapListFragment布局包含片段和两个按钮,片段是动态添加的。

map_list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/events_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@android:color/white" >

<LinearLayout
    android:id="@+id/fragments_container"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:orientation="horizontal" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center|bottom"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:textColor="@android:color/white"
        style="?android:attr/buttonBarButtonStyle"/>

    <Button
        android:id="@+id/button_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@android:color/white"
        style="?android:attr/buttonBarButtonStyle" />

    </LinearLayout>

0 个答案:

没有答案
相关问题