以编程方式从自定义布局中删除ImageView
问题描述:
我使用FlowLayout。外面我有这么一个button。以编程方式从自定义布局中删除ImageView
按下在布局中按钮将相同的画面多次按下按钮。我想出了如何添加图片,但如何删除图片?
numberButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
// Добавляем новый ImageView
if (oldValue < newValue) {
ImageView imageView = new ImageView(CreateNewTripActivity.this);
imageView.setImageResource(R.drawable.i_travel_logo_1);
ViewGroup.LayoutParams imageViewLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageViewLayoutParams.height = 300;
imageViewLayoutParams.width = 300;
imageView.setLayoutParams(imageViewLayoutParams);
imageView.setId();
flowLayout.addView(imageView);
} else {
//Удаляем
AlertDialog.Builder ad = new AlertDialog.Builder(CreateNewTripActivity.this);
ad.setTitle(getResources().getString(R.string.title_delete_person)); // заголовок
ad.setMessage(getResources().getString(R.string.dialog_aushure)); // сообщение
ad.setPositiveButton(getResources().getString(R.string.button_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(CreateNewTripActivity.this, "Вы сделали правильный выбор",
Toast.LENGTH_LONG).show();
//TO DO.......
flowLayout.removeView(imageView);
}
});
ad.setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(CreateNewTripActivity.this, "Возможно вы правы", Toast.LENGTH_LONG)
.show();
}
});
ad.setCancelable(false);
}
}
});
答
要删除添加到您的FlowLayout最后ImageView的,你只需要做到这一点:
flowLayout.removeViewAt(flowLayout.getChildCount() - 1);
可以被添加到每个imageview的唯一ID?类似的东西? //我读了关于GONE选项。我想删除所有相同的内容,而不是隐藏外观。 –
除了GONE选项,你有没有尝试做imageView = null并通过System.gc()调用GC;你可以让imageView成为一个弱引用对象。 –