casraf
casraf

Reputation: 21684

My fragments are overlapping each other

The other solutions on the website search didn't work for me.

I have one fragment which is a static row, which appears on top, and another repeating one (by db rows).

This is how it looks:

Oops

As you can see, they all overlap (there should be 3 total rows, the editable text ones, and 2 with the image and number - as you can see 1 and 2 overlap perfectly).

Here are the XMLs:

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

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_product_name"
        android:layout_weight="1"
        android:hint="@string/product_name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"
        android:ems="10"
        android:id="@+id/edit_product_quantity"
        android:layout_weight="1"
        android:hint="@string/product_quantity_short"
        android:width="10dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/product_add"
        android:id="@+id/btn_add" />
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment"
    android:id="@+id/fragment_product_row">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/img_product_thumbnail"
        android:contentDescription="@string/thumbnail"
        android:layout_gravity="center_vertical" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/product_name"
        android:id="@+id/txt_product_name"
        android:layout_gravity="center_vertical"
        android:paddingStart="10dp"
        android:paddingEnd="10dp" />
</LinearLayout>

And here is my population:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.container, new AddProductFragment());
try {
    // get all products
    Product p = new Product();
    p.findAll(this.getBaseContext(), null);
    Cursor c = p.getCursor();
    Bundle b;
    ProductRowFragment productRowFragment;

    // run through products
    if (c.getCount() > 0) {
        do {
            p.populateFields();

            // pass arguments to fragment
            b = new Bundle();
            b.putString(ProductRowFragment.PRODUCT_NAME, Integer.toString(p.getId()));

            productRowFragment = new ProductRowFragment();
            productRowFragment.setArguments(b);

            // create fragment
            transaction.add(R.id.container, productRowFragment);
        } while (c.moveToNext());
    }
} catch (Exception e) {
    Log.e("BagIt.FillProductList", e.getMessage());
}
transaction.commit();

Upvotes: 0

Views: 72

Answers (1)

John J Smith
John J Smith

Reputation: 11923

Your fragments are ontop of each other because your adding them both to the same container:

transaction.add(R.id.container, productRowFragment);

You would need to add them to different containers which are laid out below one another. Alternatively, if your going to have several and/or the number of rows can be varied, then I would suggest you find a way to populate a listview with your rows.

Upvotes: 1

Related Questions