2016-07-20 31 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); 


    } 
相关问题