3

I have a LinearLayout which has 3 containers (also LinearLayouts), and these have weight=1. Here is that layout:

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

In each of these containers I add by 1 fragment:

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

So now they are positioned like this:

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

When I click on a button I want to hide first to fragments together with their containers and show new fragment that is on the right of fragment3. So I would have something like this:

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

When I click on the button I use this to hide the fragments:

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

It hides the fragments together with their containers but the screen I get looks like this:

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

Here empty means totally empty, no fragment, no container, nothing, just empty space there.

So, my question is how to hide the fragments without leaving the blank space there?

4

2 回答 2

2

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

  • 而不是在我的布局中使用weighti 。wrap_content
  • 当我添加我的片段时,我得到我的屏幕宽度并将每个片段容器的宽度设置为1/3整个宽度。这样我得到的效果与weight=1.
  • 我没有隐藏和显示片段,而是执行了以下操作:
    • 我将第四个片段添加到第三个片段的右侧。
    • 我添加了水平滚动视图,以便用户可以左右滚动并查看所有片段。
于 2013-11-28T12:41:17.340 回答
1

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

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

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

fragmentTransaction.mFragment.hide.commit()

我发现在您也需要容器的情况下,这在很大程度上是无用的。

于 2015-10-17T07:01:56.780 回答