【问题标题】:Android - show/hide fragment leaves an empty areaAndroid - 显示/隐藏片段留下空白区域
【发布时间】:2012-05-28 07:26:32
【问题描述】:

给定:

  1. 屏幕上的两个垂直放置的元素(ViewPager 和 Fragment)
  2. 当前选中的第一个片段 (ViewFlipper) 中的操作在顶部片段中基于文本和基于 WebView 的视图之间切换,并隐藏/显示底部片段。

观察到:

  1. 隐藏底部片段会留下底部片段所在的空白空间。

我尝试了 RelativeLayout 和 LinearLayout(顶部片段设置为 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() ?
  • 不是真的,但是如果我颠倒顺序并显示文本版本/隐藏底部它会正确显示
  • Android 的 WebView 太奇怪了。您能否附上一些屏幕截图,以便我可以更具体地看到您在说什么?如果您希望它们占用一半的可用空间,您还需要在两个视图上设置 layoutWeight = 1。您还需要确保在父视图中指定orientation="vertical"。
  • 不 - 这不是 WebView,抱歉。文本也会留下一个空白空间。我会在问题中添加一些代码
  • 你是躲在View.GONE还是View.INVISIBLE?

标签: android webview show-hide fragment


【解决方案1】:

一个月后,我想出了办法。事实证明,仅仅隐藏片段是不够的,基本上我需要隐藏/显示包含片段的外壳。它仍然是最初在 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();
}

请注意,您仍然希望显示/隐藏片段以使其正常工作

【讨论】:

  • 你是怎么弄到shell的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2012-12-30
相关资源
最近更新 更多