allow selecting multiple items at the directories page
This commit is contained in:
parent
b253b4473b
commit
75d2a4a19e
8 changed files with 116 additions and 13 deletions
|
@ -0,0 +1,46 @@
|
|||
package com.simplemobiletools.gallery;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
public class MyRelativeLayout extends RelativeLayout {
|
||||
private Drawable mForegroundSelector;
|
||||
|
||||
public MyRelativeLayout(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public MyRelativeLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
{
|
||||
mForegroundSelector = getResources().getDrawable(R.drawable.selector);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawableStateChanged() {
|
||||
super.drawableStateChanged();
|
||||
mForegroundSelector.setState(getDrawableState());
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
mForegroundSelector.setBounds(0, 0, w, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void dispatchDraw(Canvas canvas) {
|
||||
super.dispatchDraw(canvas);
|
||||
mForegroundSelector.draw(canvas);
|
||||
}
|
||||
}
|
|
@ -4,15 +4,19 @@ import android.Manifest;
|
|||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
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.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
import android.widget.GridView;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
@ -28,9 +32,12 @@ import java.util.LinkedHashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
|
||||
public class MainActivity extends AppCompatActivity
|
||||
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener {
|
||||
private final int STORAGE_PERMISSION = 1;
|
||||
private List<Directory> dirs;
|
||||
private GridView gridView;
|
||||
private int selectedItemsCnt;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -69,9 +76,10 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
|
|||
dirs = new ArrayList<>(getDirectories().values());
|
||||
final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs);
|
||||
|
||||
final GridView gridView = (GridView) findViewById(R.id.directories_grid);
|
||||
gridView = (GridView) findViewById(R.id.directories_grid);
|
||||
gridView.setAdapter(adapter);
|
||||
gridView.setOnItemClickListener(this);
|
||||
gridView.setMultiChoiceModeListener(this);
|
||||
}
|
||||
|
||||
private Map<String, Directory> getDirectories() {
|
||||
|
@ -109,4 +117,44 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
|
|||
intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath());
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
|
||||
if (checked)
|
||||
selectedItemsCnt++;
|
||||
else
|
||||
selectedItemsCnt--;
|
||||
|
||||
if (selectedItemsCnt > 0)
|
||||
mode.setTitle(String.valueOf(selectedItemsCnt));
|
||||
|
||||
mode.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
final MenuInflater inflater = mode.getMenuInflater();
|
||||
inflater.inflate(R.menu.directories_menu, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
selectedItemsCnt = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanCompleted(String path, Uri uri) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,6 @@ public class PhotosActivity extends AppCompatActivity
|
|||
private int selectedItemsCnt;
|
||||
private GridView gridView;
|
||||
private String path;
|
||||
private PhotosAdapter adapter;
|
||||
private Snackbar snackbar;
|
||||
private boolean isSnackbarShown;
|
||||
private List<String> toBeDeleted;
|
||||
|
@ -95,7 +94,7 @@ public class PhotosActivity extends AppCompatActivity
|
|||
if (isDirEmpty())
|
||||
return;
|
||||
|
||||
adapter = new PhotosAdapter(this, photos);
|
||||
final PhotosAdapter adapter = new PhotosAdapter(this, photos);
|
||||
gridView = (GridView) findViewById(R.id.photos_grid);
|
||||
gridView.setAdapter(adapter);
|
||||
gridView.setOnItemClickListener(this);
|
||||
|
@ -207,6 +206,7 @@ public class PhotosActivity extends AppCompatActivity
|
|||
|
||||
private void updateGridView() {
|
||||
if (!isDirEmpty()) {
|
||||
final PhotosAdapter adapter = (PhotosAdapter) gridView.getAdapter();
|
||||
adapter.updateItems(photos);
|
||||
}
|
||||
}
|
||||
|
@ -234,7 +234,7 @@ public class PhotosActivity extends AppCompatActivity
|
|||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
final MenuInflater inflater = mode.getMenuInflater();
|
||||
inflater.inflate(R.menu.cab, menu);
|
||||
inflater.inflate(R.menu.photos_menu, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,12 +9,11 @@ import android.widget.ImageView;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.simplemobiletools.gallery.Directory;
|
||||
import com.simplemobiletools.gallery.R;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DirectoryAdapter extends BaseAdapter {
|
||||
private final Context context;
|
||||
private final List<Directory> dirs;
|
||||
|
@ -40,7 +39,7 @@ public class DirectoryAdapter extends BaseAdapter {
|
|||
Directory dir = dirs.get(position);
|
||||
holder.dirName.setText(dir.getName());
|
||||
holder.photoCnt.setText(String.valueOf(dir.getPhotoCnt()));
|
||||
Glide.with(context).load(dir.getThumbnail()).centerCrop().crossFade().into(holder.dirThumbnail);
|
||||
Glide.with(context).load(dir.getThumbnail()).placeholder(R.color.tmb_background).centerCrop().crossFade().into(holder.dirThumbnail);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:color/black"
|
||||
android:choiceMode="multipleChoiceModal"
|
||||
android:columnWidth="@dimen/dir_tmb_size"
|
||||
android:horizontalSpacing="1dp"
|
||||
android:numColumns="auto_fit"
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
<com.simplemobiletools.gallery.MyRelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/tmb_background">
|
||||
android:foreground="@drawable/selector">
|
||||
|
||||
<com.simplemobiletools.gallery.MyImageView
|
||||
android:id="@+id/dir_thumbnail"
|
||||
|
@ -35,4 +35,4 @@
|
|||
android:textColor="@android:color/white"/>
|
||||
|
||||
</RelativeLayout>
|
||||
</RelativeLayout>
|
||||
</com.simplemobiletools.gallery.MyRelativeLayout>
|
||||
|
|
9
app/src/main/res/menu/photos_menu.xml
Normal file
9
app/src/main/res/menu/photos_menu.xml
Normal 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>
|
Loading…
Reference in a new issue