Reputation: 123
I am new to android developer, after completing some example apps, I started to work grid view concepts. Here I'm trying to implement the following logics.
1.When long press happened upon images, selected images should be highlighted. 2.Need to know the selected images to delete the selected images.
I have completed to select the images from gallery, Now I need to long press images and delete the same using delete button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_browse"
android:text="Browse"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_delete"
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="660dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
//Adapter class
class ImageAdapter extends BaseAdapter {
//Context c|ass is use to bind xm| with java
private Context mcontext;
ArrayList<Uri> ImageUriList = new ArrayList<Uri>();
//Right c|ick and generate constructor
//imp|ement methods then auto generate
//Constructor ...Rigth c|ick and generate constructor
public ImageAdapter(Context mcontext, ArrayList<Uri> image) {
this.mcontext = mcontext;
ImageUriList = image;
}
@Override
public int getCount() {
return (ImageUriList == null) ? 0 : ImageUriList.size();
}
@Override
public Object getItem(int position) {
return ImageUriList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView =new ImageView(mcontext);
imageView.setImageURI(ImageUriList.get(position));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(340,340));
return imageView;
}
}
// MainActivity
public class MainActivity extends AppCompatActivity {
public static final int BROWSE_RESULT = 1;
ImageAdapter imageAdapter;
ArrayList<Uri> imagesUri = new ArrayList<Uri>();
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Button btn_browse;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView= findViewById(R.id.grid_view);
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
// imagesUri.remove(i) ;
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
//imageAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, i + " Value ", Toast.LENGTH_SHORT).show();
return false;
}
});
btn_browse = findViewById(R.id.btn_browse);
btn_browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
f_openImageExplorer();
}
});
}
public void f_openImageExplorer() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, BROWSE_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BROWSE_RESULT) {
if (resultCode == MainActivity.RESULT_OK) {
if (data.getClipData() != null) {
Toast.makeText(this,Integer.toString(resultCode) , Toast.LENGTH_SHORT).show();
int imageCount = data.getClipData().getItemCount();
for(int i = 0; i< imageCount;i++){
Uri imageUri = data.getClipData().getItemAt(i).getUri();
imagesUri.add(imageUri);
imageAdapter= new ImageAdapter(getBaseContext(), imagesUri);
gridView.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
}
}
else
{
Uri singleImageUri = data.getData();
imagesUri.add(singleImageUri);
ImageAdapter imageAdapter= new ImageAdapter(getBaseContext(), imagesUri);
gridView.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
}
}
}
}
}
Upvotes: 1
Views: 682
Reputation: 131
In your MainActivity
you can track of all clicked images and save their positions in a separated ArrayList
- and on your delete Button
you can check the positions of clicked items and remove them from the main list - after that action is done , just update your ImageAdapter
with the new list:
ArrayList<int> clickedImagesIdx = new ArrayList<int>();
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
...
clickedImagesIdx.add(i);
return false;
}
});
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickedImagesIdx.size() > 0) {
for (int i = 0; i < clickedImagesIdx.size(); i++) {
imagesUri.remove(i);
}
clickedImagesIdx.clear();
// here you will need to submit the new Uri list to your adapter
// and use notifyDataSetChange();
}
}
});
Upvotes: 1