Given:
Observed:
I tried both Relative and LinearLayout (with top fragment set to weight=1
) but both have no effect after bottom fragment is removed I still have empty space on the bottom
Here's top level layout file:
<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>
Here's code that toggles the fragment
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();
And here's how it looks:
A month later I figured out how to do it. Turns out that simply hiding the fragment is not enough, basically I need to hide/show the shell containing the fragment. It is still the LinearLayout that was originally defined in XML. In fact, it's sufficient to set visibility on that original layout to show/hide the fragment. So the code from the question now looks like this:
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();
}
Note that you still want to show/hide the fragment for this to work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With