【问题标题】:Add two different fragments to two layouts with the same body tag?将两个不同的片段添加到具有相同主体标签的两个布局中?
【发布时间】:2019-10-14 10:32:33
【问题描述】:

我正在尝试将两个不同的片段添加到我的活动中的两个容器中。这些容器是我制作的可折叠视图的一部分:

collapsible_view.xml

    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/collapsible_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:gravity="center"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/collapsible_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我尝试使用的容器是collapsible_body

我想将其中两个视图添加到我的活动布局中,然后在每个 collapsible_body 中添加不同的片段。但是,使用fragmentTransaction.replace(R.id.collapsible_body...) 并没有指定要替换的两个视图collapsible_body 之一。

基本上和这里的这个问题一样:Fragment - replace container, if id is not unique

【问题讨论】:

  • 老兄很难理解你的问题。为什么不能对两个片段使用两个容器?
  • @user3783123 抱歉,我措辞不好。这两个容器具有相同的 ID (R.id.collapsible_body)。
  • 感谢您的澄清。马上看我的答案。

标签: java android android-studio android-fragments fragmenttransaction


【解决方案1】:

您应该将相同的 id 布局包装在父布局中。然后将第二个容器的 id 设置为不同的整数。例如布局文件如下所示:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parent_one">

    <include layout="@layout/container" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parent_two">

    <include layout="@layout/container"/>

</LinearLayout>

</LinearLayout>

您将首先使用父 ID 访问它们。然后将第二个容器的 id 设置为不同的。例如:

    val containerOneId = parent_one.container.id
    val containerTwoId = 1
    parent_two.container.id = containerTwoId

    openFragment(TestFragment().apply {
        val b = Bundle()
        b.putString(TestFragment.ARG, "Fragment one")
        arguments = b
    }, containerOneId)

    openFragment(TestFragment().apply {
        val b = Bundle()
        b.putString(TestFragment.ARG, "Fragment two")
        arguments = b
    }, containerTwoId)

相同的访问逻辑,但在 Java 中(如果您不使用 Kotlin)

    LinearLayout parentOne = (LinearLayout) findViewById(R.id.parent_one);
    LinearLayout parentTwo = (LinearLayout) findViewById(R.id.parent_two); 

    FrameLayout containerOne = parentOne.findViewById(R.id.container);
    FrameLayout containerTwo = parentTwo.findViewById(R.id.container);

    int containerOneId = containerOne.getId();
    int containerTwoId = 1;
    containerTwo.setId(containerTwoId);

    openFragment(new TestFragment(), containerOneId);
    openFragment(new TestFragment(), containerTwoId);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多