Java 我的碎片相互重叠
网站搜索的其他解决方案对我不起作用 我有一个片段,它是一个静态行,显示在顶部,另一个片段是重复的(按db行) 这就是它的样子: 如您所见,它们都重叠(总共应该有3行,可编辑文本行,2行带有图像和数字-您可以看到1行和2行完全重叠) 以下是XML:Java 我的碎片相互重叠,java,android,android-fragments,Java,Android,Android Fragments,网站搜索的其他解决方案对我不起作用 我有一个片段,它是一个静态行,显示在顶部,另一个片段是重复的(按db行) 这就是它的样子: 如您所见,它们都重叠(总共应该有3行,可编辑文本行,2行带有图像和数字-您可以看到1行和2行完全重叠) 以下是XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layo
<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>
您的片段彼此重叠,因为您将它们添加到同一容器中:
transaction.add(R.id.container, productRowFragment);
您需要将它们添加到彼此下方布置的不同容器中。或者,如果您将有多个行和/或行数可以不同,那么我建议您找到一种方法,用行填充列表视图。您的容器外观如何(R.id.container
)?它是否可能不是一个线性布局
?
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);