18

My activity declares all of its GUI fragments in a single XML layout. It only needs to display a few of the fragments at launch time; the rest get shown as the user interacts with the app. A portion of the layout is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/map_panel"
        android:name="com.example.MapPanel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/map_panel_height" />
    <fragment
        android:id="@+id/list_panel"
        android:name="com.example.ListPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/map_panel" />
    <fragment
        android:id="@+id/detail_panel"
        android:name="com.example.DetailPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/map_panel"
        android:visibility="gone" />

My intention is that the list_panel fragment is visible at startup, and the detail_panel fragment is hidden until the user selects something from the list.

By default, a fragment starts out with the isHidden attribute as false. That means my activity has to iterate through the loaded fragments and manually call isHidden(true) on fragments like detail_panel at startup time.

I would prefer to declare the isHidden status in the XML layout. However, setting android:visibility="gone" in a <fragment> declaration does not change the isHidden status, and I can't find any documentation on another attribute that would do the trick.

Is it possible to set an XML attribute on a <fragment> to cause it to be hidden?

Note: I'm not concerned with view visibility, I'm concerned with the fragment.isHidden() value. That affects how FragmentManager manipulates the back stack and performs animations. If you call transaction.show(fragment) on a fragment whose view is invisible or gone, but the fragment.isHidden() value is false, then the FragmentManager will not make the view visible. See http://developer.android.com/reference/android/app/Fragment.html#isHidden() for reference.

4

5 回答 5

13

我遇到了类似的情况,我不得不隐藏一个片段。

我只是将片段包含在 LinearLayout 中,并将布局标记为可见/消失。

<LinearLayout
    android:id="@+id/map_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
于 2015-06-19T15:14:05.727 回答
9

根据 Jyo 的帖子,使用这个:

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(mFragment);
fragmentTransaction.commit();

这在 API 级别 23 上对我有用。mFragment是您要隐藏的片段。

于 2015-12-08T01:54:47.223 回答
0

This answer is a tad late thought it may be helpful for future reference. Visibility is part of the View class - Fragment extends object though not having access to the visibility values. A possibility is making the Fragment a child of a FrameLayout and calling invisible or gone on the layout. This will cause the fragment to appear to be hidden.

Hope it helps!

于 2014-09-08T13:38:52.697 回答
0
public void showHideFrgament(final Fragment fragment){

    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.setCustomAnimations(android.R.animator.fade_in,
                            android.R.animator.fade_out);

    if (fragment.isHidden()) {
                    ft.show(fragment);
                    Log.d("hidden","Show");
                } else {
                    ft.hide(fragment);
                    Log.d("Shown","Hide");                        
                }
                ft.commit();

 }
于 2015-03-24T10:09:44.533 回答
-1

我们有 isVisible 方法用于片段查看可见性 Gone 不占用任何空间 Invisble 占用实际视图空间。

于 2013-11-15T03:21:30.233 回答