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

时间:2019-12-28 11:33:01

标签: java android android-recyclerview android-cardview

每次按下按钮都会调用此功能,它会添加一张新卡,并为其分配名称。按下卡的“ x”图标时如何删除特定的卡?

//用于在按下按钮时创建单个卡的功能

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();
}

1 个答案:

答案 0 :(得分:0)

您需要将IDs或Cardviwes自身存储在某个地方,然后为删除按钮提供ID,因此单击它时,它将为您提供被单击卡的ID,然后您可以将其删除。