动态删除视图

时间:2019-04-05 14:51:06

标签: android view onclicklistener layout-inflater

我为此进行了大量搜索,但无法解决。我有带有两个textviews和一个按钮(添加其他LinearLayout)的LinearLayout,如下所示:

         <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

             <EditText android:id="@+id/ingredientsField"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/boxes_margin"
                    android:hint="@string/ingredients"
                    android:inputType="text"
                    xmlns:android="http://schemas.android.com/apk/res/android" />

             <EditText
                    android:id="@+id/quantityField"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/boxes_margin"
                    android:hint="@string/quantity"
                    android:inputType="number"
                    />

             <com.google.android.material.button.MaterialButton
                    android:id="@+id/add_ingredient_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/add"
                    android:layout_margin="@dimen/boxes_margin"
                    app:icon="@drawable/ic_add_ingr_btn"
                    />

         </LinearLayout>

添加的布局是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="50dp">

    <EditText android:id="@+id/ingredientsField"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:hint="@string/ingredients"
        android:inputType="text"
        xmlns:android="http://schemas.android.com/apk/res/android" />

    <EditText android:id="@+id/quantityField"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:hint="@string/quantity"
        android:inputType="number"
        xmlns:android="http://schemas.android.com/apk/res/android" />

    <ImageButton
        android:id="@+id/remove_ingredient_btn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:src="@drawable/ic_remove_ing_qnt"/>

</LinearLayout>

在“活动”中,我创建了一种添加新版式的方法(并且有效),并且在其中创建了一种使用 Delete Button 删除相应版式的方法。 “删除按钮”仅在添加的第一个布局中起作用一次。这是代码:

    add_ingredient.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View rowView = inflater.inflate(R.layout.ingredient_quantity_layout, null);
            // Add the new row before the add field button.
            parentIngredientLayout.addView(rowView);

            ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);

            removeChildIngredient.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   parentIngredientLayout.removeView((View) v.getParent());
                }
            });
        }
    });

2 个答案:

答案 0 :(得分:1)

在这一行:

parentIngredientLayout.removeView((View) 
  v.getParent());

您正在尝试删除removeChildIngredient ImageButton的父级,它可能是parentIngredientLayout,我认为这不是您想要的,

您可以尝试以下方法:

parentIngredientLayout.
removeView(rowView);

但是在您的实现中,添加多个成分时可能会遇到问题,因为您要为每个新成分设置一个新的onClickListener,并且ImageButton只会删除您添加的最后一个(最后一个onClickListener设置),

相反,您可以使用List / RecyclerView或搜索其他实现,例如。将ImageButton放在膨胀版式中,这样您在添加的每个版式中都会有一个删除按钮,

然后,您应该用rowView.findViewById替换ImageButton的findViewById

此行应保持不变

parentIngredientLayout.removeView((View) v.getParent());

答案 1 :(得分:1)

您必须从上次插入的版面设置OnClickListener上的ImageButton事件。 为此,只需更改此行

ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);

ImageButton removeChildIngredient = rowView.findViewById(R.id.remove_ingredient_btn);

rowView.findViewById搜索最后插入的布局上的ImageButton,而不是包含其他布局的整个布局。

相关问题