2017-04-24 57 views
1

我想刪除最近添加的圖像,如果有人點擊減號按鈕。如何刪除最近添加的圖像上的按鈕點擊Android

我已經添加圖像一個一個加上按鈕單擊。

可以在快照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; 
      } 
     } 
    } 
} 
+0

獲得點擊的位置,並且當您創建圖像編程方式轉讓其ID從佈局 –

+0

刪除該位置視野。當點擊刪除找到該圖像的ID並將其刪除 –

+0

@VivekMishra我已添加謂詞佈局,用於添加圖像可以請告訴我如何使用這種佈局如何刪除? – Jacks

回答

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); 
} 
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); 
相關問題