In my app, I select photos from gallery and insert them inside of LinearLayout
programmatically, so I did this :
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linearImages"
android:layout_width="wrap_content"
android:layout_height="150dp">
<Button
android:layout_gravity="center"
android:id="@+id/add_btn"
android:gravity="center"
android:drawableLeft="@drawable/add_icon"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And i'm set a tag to which photo that I'm putting :
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageBitmap(bitmap);
img.setTag(tagCount);
linearImages.addView(img);
bitmapArray.add(bitmap);
tagCount++;
}
And I want to remove a Image by tapping, as I said before, the Images are added programmatically, so I need something to remove images one by one without have a static Image position.
I would suggest to use RecyclerView
instaed of LinearLayout
. The reason being you can easily get the position of the item clicked and can remove accordingly. If you want to go with your own solution, then I would suggest to add setOclickListener
on each ImageView
being added. In the listener after the click event get the imagView.getTag()
, which is the position of the image view within LinearLayout
. You can then remove the image view from the LinearLayout
by using:
ll.removeViewAt(position);// to remove view from particular position
Or if you want to remove ImageView
being clicked directly then:
ll.removeView(view)// to remove particular view
Update your code of AddNewImages It will work.
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageBitmap(bitmap);
img.setTag(tagCount);
linearImages.addView(img);
bitmapArray.add(bitmap);
tagCount++;
img.setOnClickListener(clickListner);
View.OnClickListener clickListner=new View.OnClickListener() {
@Override
public void onClick(View v) {
linearImages.removeView(v);
}
};
}
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.