【问题标题】:android: Replace fragment container inside a CoordinatorLayoutandroid:替换 CoordinatorLayout 内的片段容器
【发布时间】:2016-06-06 23:05:47
【问题描述】:

我有以下 XML:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<!--I use this include as container with the FrameLayout below-->
<!--<include layout="@layout/content_main" />-->
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.improvemybrand.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add" />

问题很简单:

当我尝试从 Coordinator 替换容器 FrameLayout 时,它不起作用,它显示新片段但也保留旧片段,在我的简单示例中,带有 Hello world 的 TextView 将保留。

要替换,我使用以下代码:

FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content_main, fragment);
        transaction.commit();

有什么想法吗?

【问题讨论】:

  • TextView 不是FragmentView,所以它不会受到FragmentTransaction 的影响。
  • 编辑:@MikeM。我应该将 TextView 包装在片段中并选择要替换的片段吗?
  • 我不确定你的意思,但FragmentTransactions 只处理Fragments 和他们的Views。如果您愿意,您可以将TextView 设为另一个FragmentView,或其中的一部分,然后事务将相应地替换它。或者,如果它不需要成为 Fragment 的一部分,您可以自行删除或隐藏它。
  • @MikeM。我想我明白了......我必须从 XML 中删除 TextView,如果我真的想要,我必须在 onCreate 中以编程方式添加,然后 FragmentTransaction 将在我的容器中添加片段。我会做一些测试,让你知道。谢谢。
  • 没问题。我应该提一下,您要替换/删除的任何Fragment 都必须动态加载——即,not 定义为布局 XML 中的&lt;fragment&gt; 元素——但听起来这就是你的计划。

标签: android android-fragments


【解决方案1】:

FragmentTransactions 只处理Fragments 和他们的Views。您在布局中定义的TextView 不是FragmentView 的(一部分),因此它不会受到FragmentTransaction 的影响,并且您的sn-p 中的Fragment 将只需添加到它上面。

您有几个选择。您可以在执行交易时自行隐藏或删除TextView。如果您最初只需要那个简单的TextView,那么这可能更可取,并且将其粘贴在Fragment 中可能会过大。您也可以简单地在Fragment 的布局上设置一个不透明的背景,这将有效地隐藏TextView

然而,最好的选择可能是将TextView 放在另一个Fragment 的布局中,该Fragment 在启动时加载。随后的事务将按照您的预期替换/删除它。请注意,您希望在运行时替换/删除的任何Fragment 都必须在您的代码中动态加载。也就是说,它们不能在 Activity 布局中的 &lt;fragment&gt; 元素中定义。

【讨论】:

    【解决方案2】:

    您应该将 TextView 移动到 Fragment 的布局中。

    像这样。

    fragment_hello_world.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            />
    
    </LinearLayout>
    

    HelloWorldFragment

    public class HelloWorldFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_hello_world, container, false);;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      相关资源
      最近更新 更多