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?
// 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,然后您可以删除该卡。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.