在Android问题中插入动态视图

在Android问题中插入动态视图,android,android-layout,android-fragments,android-view,android-button,Android,Android Layout,Android Fragments,Android View,Android Button,在我的应用程序中,我有动态创建的按钮(它们是我的类别),当我点击它们时,我想在它们下面有子类别,但它们出现在所有类别下面,请看这张照片 JAVA片段 片段XML 动态添加按钮(类别)XML 动态子类别XML 正如您在照片中看到的,问题是我希望在第一个类别按钮下添加这两个子类别XML,但我在所有类别按钮下都得到了它们…您应该按如下方式更改方法: public View onCreateView(@NonNull final LayoutInflater inflater, @Null

在我的应用程序中,我有动态创建的按钮(它们是我的类别),当我点击它们时,我想在它们下面有子类别,但它们出现在所有类别下面,请看这张照片

JAVA片段 片段XML

动态添加按钮(类别)XML

动态子类别XML


正如您在照片中看到的,问题是我希望在第一个类别按钮下添加这两个子类别XML,但我在所有类别按钮下都得到了它们…

您应该按如下方式更改方法:

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

你能说出XML的名字吗?
<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>
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;
    }