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

我有一个片段,它是一个静态行,它出现在顶部,另一个是重复的(通过数据库行)。

它是这样的:



如您所见,它们全部重叠(总共应该有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();

最佳答案

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

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


您需要将它们添加到彼此下面放置的不同容器中。或者,如果您要更改的行数和/或行数可以变化,那么我建议您找到一种用行填充列表视图的方法。

09-10 09:35
查看更多