在我的应用程序中,我有动态创建的按钮(它们是我的类别),当我单击它们时,我想在其下方创建子类别,但它们出现在所有类别的下方,请看这张照片
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_newfactor,container,false);
ln = v.findViewById(R.id.itemsContainer);
dbConnector = new DbConnector(getContext(),null,null,1);
Cursor c = dbConnector.get().rawQuery("SELECT * FROM categories",null);
while (c.moveToNext()){
final int id = c.getInt(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
final View rowView = inflater.inflate(R.layout.field_button, null);
Button fieldCreator = rowView.findViewById(R.id.fieldCreator);
fieldContainer = rowView.findViewById(R.id.field_container);
//fieldCreator.setText(name);
fieldCreator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor c = dbConnector.get().rawQuery("SELECT * FROM product WHERE category_id = " + id,null);
while (c.moveToNext()){
final View rowView = inflater.inflate(R.layout.field, null);
fieldContainer.addView(rowView);
}
}
});
ln.addView(rowView);
}
return v;
}
<ScrollView
android:layout_below="@id/customerName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp">
<LinearLayout
android:id="@+id/itemsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<Button
android:id="@+id/fieldCreator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Category"
/>
<LinearLayout
android:id="@+id/field_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fieldCreator"
android:orientation="vertical"
>
</LinearLayout>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:id="@+id/clear"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_clear_blackk_24dp" />
<TextView
android:id="@+id/product_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text=""
android:layout_weight="1"
android:textAlignment="center"
android:gravity="center_vertical"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.7"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/add"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/ic_add_circle_black_24dp"
android:onClick="onIncrease" />
<Button
android:id="@+id/remove"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="@drawable/ic_remove_circle_black_24dp"
android:onClick="onDecrease" />
</LinearLayout>
<EditText
android:id="@+id/number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:hint="@string/number"
android:inputType="number"
android:text="0"
android:textAlignment="center" />
<TextView
android:id="@+id/product_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Sub category"
android:textAlignment="center"
android:textSize="15sp"
/>
</LinearLayout>
正如您在照片中看到的那样,问题是我希望在第一个CATEGORY按钮下方添加这两个添加的SUB CATEGORY XML,但我将其添加到所有CATEGORY按钮下方...
答案 0 :(得分:1)
您应按以下方式更改方法:
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_newfactor,container,false);
ln = v.findViewById(R.id.itemsContainer);
dbConnector = new DbConnector(getContext(),null,null,1);
Cursor c = dbConnector.get().rawQuery("SELECT * FROM categories",null);
while (c.moveToNext()){
final int id = c.getInt(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
final View rowView = inflater.inflate(R.layout.field_button, null);
Button fieldCreator = rowView.findViewById(R.id.fieldCreator);
//fieldCreator.setText(name);
fieldCreator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout fieldContainer = ((RelativeLayout)v.getParent()).findViewById(R.id.field_container);
fieldContainer.removeAllViews();
Cursor c = dbConnector.get().rawQuery("SELECT * FROM product WHERE category_id = " + id,null);
while (c.moveToNext()){
final View rowView = inflater.inflate(R.layout.field, null);
fieldContainer.addView(rowView);
}
}
});
ln.addView(rowView);
}
return v;
}