android - 如何在android中单击按钮删除最近添加的图像

标签 android imageview

如果有人单击减号按钮,我想删除最近添加的图像。

我在点击加号按钮时一张一张地添加了图片。

快照中可以看到enter image description here

在加号按钮上单击图像将一张一张地添加。

想在点击减号按钮时删除最近添加的图像。

           image.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
            ImageView image = new ImageView(Water.this);
            image.setBackgroundResource(R.drawable.glass);
                predicate.addView(image);



        }
        });

        ImageView minus=(ImageView)findViewById(R.id.minus);
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ImageView image = new ImageView(Water.this);
                image.setImageBitmap(null);
                predicate.removeView(image);
                //image.setBackgroundResource(R.drawable.glass);
                //((ViewGroup) image.getParent()).removeView(image);
                //predicate.removeView(image);
            }
        }); 

xml

<TextView
            android:id="@+id/waterdescription"
            android:text="Water Intake"
            android:textSize="16dp"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:textColor="#283D65"
            android:textStyle="bold"
            android:layout_height="wrap_content"

            />

        <ImageView
            android:id="@+id/minus"
            android:layout_weight="1"
            android:layout_toRightOf="@+id/waterdescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/minus"
            />

        <ImageView
            android:id="@+id/image"
            android:layout_weight="1"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/plus"
            />

谓词布局

public class PredicateLayout extends ViewGroup {

    private int line_height;

    public PredicateLayout(Context context) {
        super(context);
    }

    public PredicateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);

        final int width = MeasureSpec.getSize(widthMeasureSpec);

        // The next line is WRONG!!! Doesn't take into account requested MeasureSpec mode!
        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
        final int count = getChildCount();
        int line_height = 0;

        int xpos = getPaddingLeft();
        int ypos = getPaddingTop();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = child.getLayoutParams();
                child.measure(
                        MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED));

                final int childw = child.getMeasuredWidth();
                line_height = Math.max(line_height, child.getMeasuredHeight() + lp.height);

                if (xpos + childw > width) {
                    xpos = getPaddingLeft();
                    ypos += line_height;
                }

                xpos += childw + lp.width + 8;
            }
        }
        this.line_height = line_height;

        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
            height = ypos + line_height;

        } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
            if (ypos + line_height < height) {
                height = ypos + line_height;
            }
        }
        setMeasuredDimension(width, height + 20);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(2, 2); // default of 1px spacing
    }

    @Override
    protected boolean checkLayoutParams(LayoutParams p) {
        return (p instanceof LayoutParams);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();
        final int width = r - l;
        int xpos = getPaddingLeft();
        int ypos = getPaddingTop();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final int childw = child.getMeasuredWidth();
                final int childh = child.getMeasuredHeight();
                final LayoutParams lp =  child.getLayoutParams();
                if (xpos + childw > width) {
                    xpos = getPaddingLeft();
                    ypos += line_height;
                }
                child.layout(xpos, ypos, xpos + childw, ypos + childh);
                xpos += childw + lp.width + 8;
            }
        }
    }
}

最佳答案

您没有引用在 plus 按钮的 onClick() 方法中添加的 imageView。

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

                ImageView image = new ImageView(Water.this);
                image.setImageBitmap(null);
                predicate.removeView(image);
                //image.setBackgroundResource(R.drawable.glass);
                //((ViewGroup) image.getParent()).removeView(image);
                //predicate.removeView(image);
            }
        });

ImageView image = new ImageView(Water.this); 在这一行中,您正在创建一个带有水的新 ImageView 并尝试将其从父布局中移除。但你甚至没有添加这个。

您需要做的是保留对您要添加的 View 以及按钮的 onClick() 方法的引用。

你可以这样做:

public class PredicateLayout extends ViewGroup {
   private LinkedList<ImageView> imageViews;

   //other parts are omitted...

   public PredicateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        imageViews = new LinkedList();
    }

   //...some other code...

   public LinkedList<ImageView> getImageViews(){
        return imageViews;
   }
}

添加时:

加号按钮:

...onClick() {
   //..
   predicate.addView(image);
   predicate.getImageViews().add(image);

}

减号按钮:

...onClick(){
  //pollLast returns last element in the list 
  ImageView lastAddedImageView = predicate.getImageViews().pollLast() 

  predicate.removeView(lastAddedImageView);
}

关于android - 如何在android中单击按钮删除最近添加的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43588748/

相关文章:

android - 在 Android 中用透明颜色填充 Canvas

android - 如何在android表格布局中动态居中图像

java - 如何在不丢失 x 和 y 布局坐标的情况下添加缩放功能

android - Android 中的圆形 ImageView ,无需使用任何外部库

使用传感器数据的 Android 智能电源管理

java - 出现异常如何解决

android - 如何在缩放或拖动(左,右,上,下)图像时准确定位另一个 View ?

android - 简单的 NFC 网址共享

android - Nexus 6 和 MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);

android - 整个 Activity 上的 OnTouchListener