Android - 显示/隐藏片段会留下一个空白区域

7
给定:
  1. 屏幕上有两个垂直放置的元素(ViewPager和Fragment)
  2. 在第一个当前选定的片段中进行操作(ViewFlipper),可以在顶部片段中在基于文本和基于WebView的视图之间切换,并隐藏/显示底部片段。
观察到:
  1. 隐藏底部片段会留下底部片段所在的空白区域。
我尝试了相对布局和线性布局(将顶部片段设置为weight=1),但在移除底部片段后两者都没有效果,仍然在底部留下空白。
以下是顶级布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="0dip" android:layout_weight="1"/>

<!-- This gets replaced with appropriate fragment at run time -->
<LinearLayout
    android:id="@+id/scrollFragmentPlaceholder"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="110dip" />
</LinearLayout>

这里是切换片段的代码

    Fragment scroll = getSupportFragmentManager().findFragmentById(R.id.scrollFragment);
    if (scroll.isHidden() == isWebView)
        return; // already handled, do nothing
    FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
    if (scroll != null && scroll.isAdded()) {
        if (isWebView) {
            tr.hide(scroll);
        } else
            tr.show(scroll);
    }
    tr.commit();

这是它的外观: 底部碎片被隐藏

你有尝试在剩余的视图上执行 *.measure() 吗? - Codeman
不是真的,但如果我反转顺序并显示文本版本/隐藏底部,它会正确显示。 - Bostone
Android的WebView非常奇怪。你能否包含一些截图,以便我更具体地了解你在说什么?此外,如果您希望两个视图占用可用空间的一半,则需要在两个视图上都设置layoutWeight = 1。还需要确保在父视图中指定orientation="vertical"。 - Codeman
不好意思,它不是WebView。Textual也会留下一个空白的空间。我会在问题中放一些代码。 - Bostone
不是我的情况。我已经有一个ViewFlipper,但它包含在Fragment中,而这个Fragment只是ViewPager中的众多Fragment之一。我需要清除那个讨厌的底部Fragment。 - Bostone
显示剩余3条评论
1个回答

16
一个月后我找到了解决方法。原来只是隐藏片段是不够的,实际上我需要隐藏/显示包含该片段的shell。这个shell其实就是在XML中最初定义的LinearLayout。事实上,只需设置原始布局的可见性即可显示/隐藏片段。因此,问题的代码现在看起来像这样:
public void onJobViewToggled(final boolean isWebView) {
    if (isFinishing())
        return;
    final Fragment scroll = getSupportFragmentManager().findFragmentById(R.id.scrollFragment);
    if (scroll.isHidden() == isWebView)
        return; // already handled, do nothing
    final FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
    if (scroll != null && scroll.isAdded()) {
        if (isWebView) {
            tr.hide(scroll);
            // shell is the original placeholder
            shell.setVisibility(View.GONE);
        } else {
            tr.show(scroll);
            shell.setVisibility(View.VISIBLE);
        }
    }
    tr.commit();
}

请注意,您仍然需要显示/隐藏该片段才能使此功能正常工作


2
你是如何获取shell的? - Nick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接