I have an image gallery app . I can select only a single image at a time right now. But I want to be able to select multiple images and deleting them at a time. I tried some code based on this problem How to do multiselect functionality on grid view items on tap? but I am not sure why it is not working. Any suggestions?
PhotosActivity.java
public class PhotosActivity extends AppCompatActivity {
int int_position;
private GridView gridView;
GridViewAdapter adapter;
ArrayList<Model_images> al_menu = new ArrayList<>();
private ArrayList<Integer> mSelected = new ArrayList<Integer>();
boolean boolean_folder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
button3.setVisibility(View.GONE);
button4.setVisibility(View.GONE);
button5.setVisibility(View.GONE);
gridView = (GridView) findViewById(android.R.id.list);
int_position = getIntent().getIntExtra("value", 0);
adapter = new GridViewAdapter(this, al_images, int_position);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String abc = "file://" + al_images.get(int_position).getAl_imagepath().get(position);
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
i.putExtra("folderPosition", int_position);
i.putExtra("abc", abc);
startActivity(i);
}
});
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
Integer position1 = new Integer(position);
if(mSelected.contains(position1)) {
mSelected.remove(position1); // remove item from list
// update view (v) state here
// eg: remove highlight
}
else {
mSelected.add(position1); // add item to list
// update view (v) state here
// eg: add highlight
}
}
Take a field selected in the model class. whenever an item is selected set selected=true and notifyAdapter .
in your onBindViewHolder() of adapter if item isSelected change background of the view (dont forget to write the else block ).
Also to delete the items maintain a separate list of the objects in the adapter or activity.
In Your Code : I would suggest rather than maintaining positions maintain list of objects . Use the above 2 steps to make change in the view of items when selected . hope it helps.