add a contextual actionbar for deleting images

This commit is contained in:
tibbi 2016-02-28 21:24:27 +01:00
parent 732fa20cb5
commit a68ad87b4b
4 changed files with 57 additions and 1 deletions

View file

@ -6,6 +6,10 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.GridView; import android.widget.GridView;
@ -19,8 +23,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener {
private List<String> photos; private List<String> photos;
private int selectedItemsCnt;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -33,6 +38,7 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos(path)); final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos(path));
gridView.setAdapter(adapter); gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this); gridView.setOnItemClickListener(this);
gridView.setMultiChoiceModeListener(this);
final String dirName = Helpers.getFilename(path); final String dirName = Helpers.getFilename(path);
setTitle(dirName); setTitle(dirName);
@ -66,4 +72,43 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
intent.putExtra(Constants.PHOTO, photos.get(position)); intent.putExtra(Constants.PHOTO, photos.get(position));
startActivity(intent); startActivity(intent);
} }
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked)
selectedItemsCnt++;
else
selectedItemsCnt--;
mode.setTitle(String.valueOf(selectedItemsCnt));
mode.invalidate();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
final MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.cab, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.cab_remove:
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
selectedItemsCnt = 0;
}
} }

View file

@ -9,4 +9,5 @@
android:horizontalSpacing="1dp" android:horizontalSpacing="1dp"
android:numColumns="auto_fit" android:numColumns="auto_fit"
android:stretchMode="columnWidth" android:stretchMode="columnWidth"
android:choiceMode="multipleChoiceModal"
android:verticalSpacing="1dp"/> android:verticalSpacing="1dp"/>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_remove"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/remove"
app:showAsAction="always"/>
</menu>

View file

@ -2,4 +2,5 @@
<string name="app_name">Simple Gallery</string> <string name="app_name">Simple Gallery</string>
<string name="share_via">Share via</string> <string name="share_via">Share via</string>
<string name="no_permissions">Not much to do in a gallery without accessing your photos</string> <string name="no_permissions">Not much to do in a gallery without accessing your photos</string>
<string name="remove">Remove</string>
</resources> </resources>