2

I have several fragments within one Activity.

I am trying to make my app compatible with both small and large screens.

I have created a main layout with a LinearLayout as the root. This LinearLayout contains two FrameLayouts. One FrameLayout is used to store Fragments which will store lists or any other side details. I only want this to be in view when specific buttons are pressed.

The other FrameLayout is used to display the main part of the app (a map) which is in its own fragment.

To begin with I add my main map fragment using:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

ft.add(R.id.viewer, viewModeFragment);
ft.commit();

When I want the side panel to appear with a list fragment I call something like this:

            FrameLayout fl = (FrameLayout)findViewById(R.id.list);
            fl.setVisibility(View.VISIBLE);

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.list, editOsmInfoFragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.addToBackStack(null);
            ft.show(editOsmInfoFragment);
            ft.commit();

Here is my XML file for the main Activity layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/list"
        android:name="com.srose.cyclopathed.view.LoadRoutesFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:visibility="gone"/>

    <FrameLayout
        android:id="@+id/viewer"
        android:name="com.srose.cyclopathed.view.ViewModeFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />    
</LinearLayout> 

This seems to work some what ok but the main problem is that if I use the app on the tablet and the side bar appears with a list fragment contained within it, if the back button is pressed the fragment vanishes as expected but the blank list FrameLayout remains on screen because it was not part of the transaction.

I guess I am not using this properly but I have no idea how to implement it so that the whole side bar whole slide to the left in the back button is pressed.

Can anyone please help? Thanks

sam
  • 2,469
  • 8
  • 37
  • 57

1 Answers1

0

Do not explicitly set the visibility on R.id.list.

In your layout XML, remove the android:visiblility attribute on R.id.list FrameLayout to make it visible. Since this FrameLayout is initially empty, it will not show up on screen. When the side panel is added to it programmatically via the FragmentTransaction, you will see it, and when it is removed (via the back button), it will go away. You must call FrameLayout# setConsiderGoneChildrenWhenMeasuring() in order for the layout to collapse when the Fragment is removed.

user697495
  • 590
  • 1
  • 3
  • 9
  • Hi, I removed that line in the xml file but when I start the app, the FrameLayout appears any way. I think I have solved most of the back stack issues now though although I was wondering if there is any way that I can animate the sidebar when it is opened so that it slides out. Is this possible using the FrameLayout setVisibity method? – sam Jan 27 '13 at 17:50
  • According to the documentation for [FrameLayout}(http://developer.android.com/reference/android/widget/FrameLayout.html), `setConsiderGoneChildrenWhenMeasuring()` in order for FrameLayout to collapse when you click back. This [SO question](http://stackoverflow.com/a/7892524/697495) addresses your animation question. – user697495 Jan 29 '13 at 06:20