动态删除视图

问题描述 投票:0回答:2

我经常搜索这个,但我无法解决它。我有LinearLayout有两个textviews和一个按钮(添加其他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>

在Activity中,我创建了一个添加新Layouts的方法(并且它可以工作),并在其中使用Delete按钮删除相应的Layout。删除按钮只能工作一次,并且只有在添加的第一个布局中才有效。这是代码:

    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());
                }
            });
        }
    });
android view onclicklistener layout-inflater
2个回答
1
投票

您必须在最后插入的布局中在OnClickListener上设置ImageButtonevent。要做到这一点,只需更改此行

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

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

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


1
投票

在这一行:

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

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

你可以试试这个:

parentIngredientLayout.
removeView(rowView);

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

相反,您可以使用List / RecyclerView或搜索其他实现,例如。将ImageButton放在Inflated Layout中,这样您在添加的每个Layout中都会有一个Remove Button,

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

这条线应保持不变

parentIngredientLayout.removeView((View) v.getParent());
© www.soinside.com 2019 - 2024. All rights reserved.