我的碎片彼此重叠

时间:2014-08-19 21:50:05

标签: java android android-fragments

网站搜索中的其他解决方案对我不起作用。

我有一个片段,它是一个静态行,显示在顶部,另一个重复一个(由db行)。

它的外观如下:

Oops

正如您所看到的,它们都是重叠的(总共应该有3行,可编辑的文本行,以及带有图像和数字的2行 - 正如您可以看到1和2完全重叠)。

以下是XML:

<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>

这是我的人口:

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();

1 个答案:

答案 0 :(得分:1)

你的片段彼此重叠,因为你将它们都添加到同一个容器中:

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

您需要将它们添加到彼此相邻的不同容器中。或者,如果您要有多个行和/或行数可以改变,那么我建议您找到一种方法来使用行填充列表视图。

相关问题