如何删除Android上按钮点击最近添加的图像

时间:2017-04-24 12:59:29

标签: 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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您没有引用您在加号按钮的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并尝试将其从父布局中删除。但你甚至没有添加它。

您需要做的是保持对您添加的视图的引用以及按钮的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);
}

答案 1 :(得分:0)

添加以下视图:

        ImageView image = new ImageView(Water.this);
        image.setId(Integer.parseInt("1234"));
        image.setBackgroundResource(R.drawable.glass);

        predicate.addView(image);

删除它:

    View rView=(ImageView)view.findViewById(Integer.parseInt("1234"));
    predicate.removeView(rView);