2016-07-20 34 views
0

我有一個循環,用於創建所有視圖的位置,並且在此循環中我有按鈕,我嘗試創建刪除1視圖(如果已選擇按鈕)。所有的視圖都在框架中,並且具有滾動視圖的線性佈局。如何在Android Studio中創建循環中的按鈕刪除視圖

FrameLayout.LayoutParams deletePartsParams = new   
FrameLayout.LayoutParams(tenPixelsWight*7,tenPixelsHeight*7); 
deletePartsParams.setMargins(tenPixelsWight * 60, tenPixelsHeight * 20, 0, 0); 
deleteOfPart.setBackgroundResource(R.drawable.delete);    //Кнопка удалить автозапчасть 
deleteOfPart.setClickable(true); 
deleteOfPart.setTag(i); 

deleteOfPart.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    } 
}); 

deleteOfPart.setLayoutParams(deletePartsParams); 
mainFrame.addView(deleteOfPart); 

回答

1

要刪除視圖首先需要找到在主要佈局視圖,通過循環,我們可以得到所有孩子的看法,但需要確定哪些需要當前視圖刪除,所以通過比較childview和刪除視圖標籤我們可以識別它,所以只需通過mainLinearLayout.removeView(view)將其刪除;方法

int count = mainLinearLayout.getChildCount(); 
View view = null; 
for(int countI=0; countI<count; countI++) { 
    view = layout.getChildAt(countI); 
    if(((int) view.getTag()) == ((int) v.getTag())){ 
     mainLinearLayout.removeView(view); 
     break; 
    } 
} 

添加上述代碼到的onClick deleteOfPart

+0

它工作!謝謝你非常匹配的幫助! – Slavik

0

對你好的一天,善良的人類。如果我對你有了很好的理解,你發佈的代碼就在for循環中,並且你在這個循環中創建了不同數量的視圖,你想在點擊它時刪除它。

內部的onClick你那裏,嘗試把mainFrame.removeView(deleteOfPart);

+0

你好的。我試圖做到這一點,但它的有趣 - 然後我點擊按鈕刪除第一次查看它刪除最後一個視圖不是第一次;)。我已經對這個觀點設置了標籤,並在Toast上檢查它們,可能是使用它們的東西?我試圖與如果(v.getTag ==標記視圖)比較以便刪除它,但不工作(((( – Slavik

+0

好吧,發佈整個循環代碼請 –

0
 // Заполняем нашими вьюшками и подставляем значения из РейЛиста с данными 
    for ( i =0; i<counter;i++) { 
     ImageView picture = new ImageView(this); // Фотография автозапчасти 
     EditText changeOfQuantity = new EditText(this); // Едит Текст с изменением количества автозапчастей в заказе 
     ImageView editeOfQuantity = new ImageView(this); // Кнопка редактировать количество автозапчастей 
     deleteOfPart = new ImageView(this); // Кнопка удалить автозапчасть из корзины 
     TextView Number = new TextView(this); // Номер автозапчасти 
     TextView Description = new TextView(this); //Создаем Вьюшку текста Описание автозапчасти 
     TextView Price = new TextView(this); //Создаем Вьюшку текста цены автозапчасти 
     TextView Brand = new TextView(this); //Создаем Вьюшку текста бренда автозапчасти 
     TextView deliveryOfTime = new TextView(this); //Срок доставки 
     mainFrame = new FrameLayout(this); //Параметры ФреймЛайаута 

     LinearLayout.LayoutParams frameLayoutFirstCartParams = new LinearLayout.LayoutParams(tenPixelsWight*80,tenPixelsHeight*50); 
     frameLayoutFirstCartParams.setMargins(0, tenPixelsHeight * 4, 0, 0);   //ФреймЛайаут 
     mainFrame.setTag(i); 
     mainFrame.setLayoutParams(frameLayoutFirstCartParams); 
     mainLinearLayout.addView(mainFrame); 

     FrameLayout.LayoutParams frameLayoutPictureParams = new FrameLayout.LayoutParams(tenPixelsWight*15, tenPixelsHeight*15); 
     frameLayoutPictureParams.setMargins(tenPixelsWight/2, tenPixelsHeight, 0, 0); 
     picture.setLayoutParams(frameLayoutPictureParams); // Фотография автозапчастей 
     picture.setBackgroundResource(R.drawable.camera); 
     mainFrame.addView(picture); 

     FrameLayout.LayoutParams numberOfPartParams = new FrameLayout.LayoutParams(tenPixelsWight*45, tenPixelsHeight*5); 
     numberOfPartParams.setMargins(tenPixelsWight * 20, tenPixelsHeight * 11, 0, 0); 
     Number.setLayoutParams(numberOfPartParams); // Артикул автозапчасти 
     Number.setText("Номер запчасти: " + article.get(i)); 
     mainFrame.addView(Number); 

     FrameLayout.LayoutParams frameLayoutDeliveryTextParams = new FrameLayout.LayoutParams(tenPixelsWight * 45, tenPixelsHeight*5); 
     frameLayoutDeliveryTextParams.setMargins(tenPixelsWight * 20, tenPixelsHeight * 6, 0, 0); 
     deliveryOfTime.setTextSize(15);   // Cрок доставки автозапчасти 
     deliveryOfTime.setText("Cрок доставки: " + delivery.get(i) + " дней"); 
     deliveryOfTime.setLayoutParams(frameLayoutDeliveryTextParams); 
     mainFrame.addView(deliveryOfTime); 

     FrameLayout.LayoutParams frameLayoutPriceTextParams = new FrameLayout.LayoutParams(tenPixelsWight * 25, tenPixelsHeight*5); 
     frameLayoutPriceTextParams.setMargins(tenPixelsWight * 60, tenPixelsHeight * 10, 0, 0); 
     Price.setTextColor(Color.parseColor("#D50000"));  // Цена автозапчасти 
     Price.setTextSize(20); 
     Price.setText(price.get(i)); 
     Price.setLayoutParams(frameLayoutPriceTextParams); 
     mainFrame.addView(Price); 

     FrameLayout.LayoutParams frameLayoutBrandTextParams = new FrameLayout.LayoutParams(tenPixelsWight*20, tenPixelsHeight*5); 
     frameLayoutBrandTextParams.setMargins(tenPixelsWight * 20, tenPixelsHeight * 15, 0, 0); 
     Brand.setTextColor(Color.parseColor("#D50000"));  // Бренд автозапчасти 
     Brand.setTextSize(20); 
     Brand.setText(brand.get(i)); 
     Brand.setLayoutParams(frameLayoutBrandTextParams); 
     mainFrame.addView(Brand); 

     FrameLayout.LayoutParams frameLayoutEditQuantityParams = new FrameLayout.LayoutParams(tenPixelsWight*50,tenPixelsHeight*10); 
     frameLayoutEditQuantityParams.setMargins(tenPixelsWight/2, tenPixelsHeight * 20, 0, 0); 
     changeOfQuantity.setHint("Изменить количество"); // Изменение количества автозапчастей в детали 
     changeOfQuantity.setLayoutParams(frameLayoutEditQuantityParams); 
     mainFrame.addView(changeOfQuantity); 

     FrameLayout.LayoutParams frameLayoutDescriptionParams = new FrameLayout.LayoutParams(tenPixelsWight*50,tenPixelsHeight*5); 
     frameLayoutDescriptionParams.setMargins(tenPixelsWight * 20, tenPixelsHeight/4, 0, 0); 
     Description.setText(description.get(i)); 
     Description.setTextSize(20);          // Описание автозапчасти 
     Description.setTextColor(Color.parseColor("#000000")); 
     Description.setLayoutParams(frameLayoutDescriptionParams); 
     mainFrame.addView(Description); 

     FrameLayout.LayoutParams EditQuantityParams = new FrameLayout.LayoutParams(tenPixelsWight*7,tenPixelsHeight*7); 
     EditQuantityParams.setMargins(tenPixelsWight * 52, tenPixelsHeight * 20, 0, 0); 
     editeOfQuantity.setClickable(true); 
     editeOfQuantity.setBackgroundResource(R.drawable.undo);   //Кнопка исправить количество 
     editeOfQuantity.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 
     editeOfQuantity.setLayoutParams(EditQuantityParams); 
     mainFrame.addView(editeOfQuantity); 


     FrameLayout.LayoutParams deletePartsParams = new FrameLayout.LayoutParams(tenPixelsWight*7,tenPixelsHeight*7); 
     deletePartsParams.setMargins(tenPixelsWight * 60, tenPixelsHeight * 20, 0, 0); 
     deleteOfPart.setBackgroundResource(R.drawable.delete);    //Кнопка удалить автозапчасть 
     deleteOfPart.setClickable(true); 
     deleteOfPart.setTag(i); 

     deleteOfPart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mainFrame.removeView(deleteOfPart); 

      } 
     }); 

     deleteOfPart.setLayoutParams(deletePartsParams); 
     mainFrame.addView(deleteOfPart); 


    } 
相關問題