jpprade
jpprade

Reputation: 3664

Only first fragment showing

I am trying to create an activity with 2 Fragment

When I declare the fragment in the view it works. But now I want to manage fragment dynamically :

GalleriesFragment gfs = new GalleriesFragment();
GalleryFragment gf = new GalleryFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.layout_m,gfs);
ft.add(R.id.layout_m,gf);
ft.commit();

My Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:id="@+id/layout_m">
</LinearLayout>

When doing this only the first fragment added is shown, I tried to invert their order when adding fragment...

When I was working on the layout I could set my fragment width with:

android:layout_width="400dp"

Now I have 2 questions :

  1. How can I make my 2 fragments to display?
  2. How can I set the width (programmatically?) of the 1st fragment and the second to fill the remaining width?

Thanks for your help!

PS:

GalleriesFragment doesn't have a layout and is not overriding onCreateView, but has an ArrayAdapter returning view like this for each rows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/idimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10sp"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="10sp"
        android:contentDescription="preview"
         >
    </ImageView>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/texttitre"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="7sp"
            android:layout_marginTop="10sp"
            android:background="@layout/roundcorner"
            android:gravity="center"
            android:text="Title here" >
        </TextView>

        <TextView
            android:id="@+id/itemdescription"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:text="description" >
        </TextView>

    </LinearLayout>

</LinearLayout>

GalleryFragment :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Update 2 :

I added an onCreateView in my GalleriesFragment class :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        FrameLayout.LayoutParams lparams =  new FrameLayout.LayoutParams(400, LayoutParams.MATCH_PARENT);
        container.setLayoutParams(lparams);
        return super.onCreateView(inflater, container, savedInstanceState);
}

This fixes the first fragment's width, but the second one is still not showing...

Upvotes: 3

Views: 1721

Answers (1)

user
user

Reputation: 87064

How can I make my 2 fragments to display ?

Your current approach will make the first fragment supplied to the add method to take all the space of your container layout. One solution is to use a layout that reserves space for each of the two fragments that get added, something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_m"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="100dp"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/frame2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Then in code:

ft.add(R.id.frame1, gfs);
ft.add(R.id.frame2, gf);
ft.commit();

How can I set the width (programmatically ?)of the 1st fragment and the second to fill the remaining width?

Fragments are not just ordinary views but one way to do that is to use your single LinearLayout layout file and set the LayoutParams of the fragment's view in onActivityCreated:

getView().setLayoutParams(new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.WRAP_CONTENT));

Of course this will make your fragments to be tied to the parent container, also I don't know how well this will work in the overall fragment system.

Upvotes: 2

Related Questions