简体   繁体   English

在动态创建的卡片视图上,如果按下特定于卡片的删除按钮,如何删除单个卡片?

[英]On a card view created dynamically, how can I remove individual cards if a delete button specific to a card is pressed?

This function is called each time button is pressed and it adds a new card having the name assigned to that card.每次按下按钮时都会调用此函数,并添加一张具有分配给该卡的名称的新卡。 How can I remove a specific card when the 'x' icon of the card is pressed?按下卡片的“x”图标时,如何移除特定卡片?

// function to create individual cards on button press // 在按钮按下时创建单张卡片的函数

public void cardview1()
{
    text2 = v1.findViewById(R.id.dev_name);
    dev_id1 = v1.findViewById(R.id.dev_id);

    cardview2 = new CardView(getApplicationContext());
    cardview2.setFocusable(false);
    cardview2.setClickable(false);

    layoutparams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutparams1.setMargins(19, 83, 19, 0);

    lay1 = new LinearLayout(this);
    lay1.setOrientation(LinearLayout.VERTICAL);

    layoutparams2 = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 115);
    lay2 = new LinearLayout(this);
    lay2.setLayoutParams(layoutparams2);
    lay2.setOrientation(LinearLayout.HORIZONTAL);

    layoutparams3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    lay6 = new LinearLayout(this);
    lay6.setWeightSum(12);
    lay6.setLayoutParams(layoutparams3);
    lay6.setPadding(10,40,0,0);
    lay6.setOrientation(LinearLayout.HORIZONTAL);

    cardview2.setLayoutParams(layoutparams1);
    cardview2.setCardBackgroundColor(Color.parseColor("#2fffffff"));
    cardview2.setMinimumHeight(330);
    cardview2.addView(lay1);

    dev_text1 = new TextView(getApplicationContext());
    dev_text1.setText(dev_name1.getText().toString().trim()+"  ");
    dev_text1.setPadding(20, 15,0,0);
    dev_text1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 26);
    dev_text1.setTypeface(null, Typeface.BOLD);
    dev_text1.setTextColor(Color.GRAY);

    sw0 = new Button(getApplicationContext());
    sw0.setText("  X");
    sw0.setTextSize(20);
    sw0.setTypeface(null, Typeface.BOLD);
    sw0.setBackgroundResource(0);
    sw0.setTextColor(Color.GRAY);
    sw0.setPadding(0,0,0,5);
    sw0.setLayoutParams(new LinearLayout.LayoutParams(70, 70));

    lay1.addView(lay2);
    lay1.addView(lay6);
    lay2.addView(dev_text1);
    lay2.addView(sw0);
    ahomeLayout.removeView(atv);
    ahomeLayout.addView(cardview2);

    saveInPref();
}

您需要在某处存储 Id 或 Cardviwes 本身,然后为删除按钮提供 ID,因此当它被点击时,它将为您提供被点击卡的 ID,然后您可以删除该卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM