隐藏片段留空



我有一个LinearLayout,它有3个容器(也是LinearLayouts),这些容器有weight=1。这是布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:divider="?android:dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle" >
    <LinearLayout
        android:id="@+id/container1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF0000"
        android:orientation="vertical" >
    </LinearLayout>
    <LinearLayout
        android:id="@+id/container2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#00FF00"
        android:orientation="vertical" >
    </LinearLayout>
    <LinearLayout
        android:id="@+id/container3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

在每个容器中,我添加 1 个片段:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container1, fragment1, TAG1);
transaction.add(R.id.container2, fragment2, TAG2);
transaction.add(R.id.container3, fragment3, TAG3);
transaction.commit();

所以现在他们的定位是这样的:

-------------------------------------
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
| fragment1 | fragment2 | fragment3 |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
-------------------------------------

当我单击一个按钮时,我想首先隐藏到片段及其容器,并显示位于fragment3右侧的新片段。所以我会有这样的东西:

-------------------------------------
|           |                       |
|           |                       |
|           |                       |
|           |                       |
|           |                       |
| fragment3 |       fragment4       |
|           |                       |
|           |                       |
|           |                       |
|           |                       |
|           |                       |
-------------------------------------

当我单击按钮时,我使用它来隐藏片段:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.hide(fragment1);
transaction.hide(fragment2);
transaction.addToBackStack(null);
transaction.commit();

它将片段及其容器隐藏在一起,但我得到的屏幕如下所示:

-------------------------------------
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|   empty   |   empty   | fragment3 |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
-------------------------------------

这里empty的意思是完全空的,没有碎片,没有容器,什么都没有,只是那里的空空间。

所以,我的问题是如何隐藏片段而不留空白?

我没有设法让它以这种方式工作,所以这是我的解决方案:

  • 我没有使用weight而是在布局中使用了wrap_content
  • 当我添加片段时,我会得到我的屏幕宽度,并将每个片段容器的宽度设置为整个宽度的1/3。这样我得到了与weight=1相同的效果。
  • 我没有隐藏和显示片段,而是做了以下操作:
    • 我将第 4 个片段添加到第 3 个片段的右侧。
    • 我添加了水平滚动视图,以便用户可以左右滚动并查看所有片段。

要隐藏他们的容器,您可以使用

View mView = findViewById(R.id.container);
mView.setVisibility(View.GONE);  // Instead of View.INVISIBLE or View.VISIBLE

这将防止您的容器占用任何空间,并且显然会隐藏该容器中的任何片段,因此

fragmentTransaction.mFragment.hide.commit()

我发现在你也需要容器消失的情况下,它基本上是无用的。

最新更新