allow selecting multiple items at the directories page

This commit is contained in:
tibbi 2016-03-06 19:05:37 +01:00
parent b253b4473b
commit 75d2a4a19e
8 changed files with 116 additions and 13 deletions

View file

@ -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);
}
}

View file

@ -4,15 +4,19 @@ import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
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.AdapterView.OnItemClickListener;
import android.widget.GridView; import android.widget.GridView;
import android.widget.Toast; import android.widget.Toast;
@ -28,9 +32,12 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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 final int STORAGE_PERMISSION = 1;
private List<Directory> dirs; private List<Directory> dirs;
private GridView gridView;
private int selectedItemsCnt;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -69,9 +76,10 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
dirs = new ArrayList<>(getDirectories().values()); dirs = new ArrayList<>(getDirectories().values());
final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); 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.setAdapter(adapter);
gridView.setOnItemClickListener(this); gridView.setOnItemClickListener(this);
gridView.setMultiChoiceModeListener(this);
} }
private Map<String, Directory> getDirectories() { private Map<String, Directory> getDirectories() {
@ -109,4 +117,44 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath()); intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath());
startActivity(intent); 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) {
}
} }

View file

@ -44,7 +44,6 @@ public class PhotosActivity extends AppCompatActivity
private int selectedItemsCnt; private int selectedItemsCnt;
private GridView gridView; private GridView gridView;
private String path; private String path;
private PhotosAdapter adapter;
private Snackbar snackbar; private Snackbar snackbar;
private boolean isSnackbarShown; private boolean isSnackbarShown;
private List<String> toBeDeleted; private List<String> toBeDeleted;
@ -95,7 +94,7 @@ public class PhotosActivity extends AppCompatActivity
if (isDirEmpty()) if (isDirEmpty())
return; return;
adapter = new PhotosAdapter(this, photos); final PhotosAdapter adapter = new PhotosAdapter(this, photos);
gridView = (GridView) findViewById(R.id.photos_grid); gridView = (GridView) findViewById(R.id.photos_grid);
gridView.setAdapter(adapter); gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this); gridView.setOnItemClickListener(this);
@ -207,6 +206,7 @@ public class PhotosActivity extends AppCompatActivity
private void updateGridView() { private void updateGridView() {
if (!isDirEmpty()) { if (!isDirEmpty()) {
final PhotosAdapter adapter = (PhotosAdapter) gridView.getAdapter();
adapter.updateItems(photos); adapter.updateItems(photos);
} }
} }
@ -234,7 +234,7 @@ public class PhotosActivity extends AppCompatActivity
@Override @Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) { public boolean onCreateActionMode(ActionMode mode, Menu menu) {
final MenuInflater inflater = mode.getMenuInflater(); final MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.cab, menu); inflater.inflate(R.menu.photos_menu, menu);
return true; return true;
} }

View file

@ -9,12 +9,11 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import java.util.List;
import com.simplemobiletools.gallery.Directory; import com.simplemobiletools.gallery.Directory;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import java.util.List;
public class DirectoryAdapter extends BaseAdapter { public class DirectoryAdapter extends BaseAdapter {
private final Context context; private final Context context;
private final List<Directory> dirs; private final List<Directory> dirs;
@ -40,7 +39,7 @@ public class DirectoryAdapter extends BaseAdapter {
Directory dir = dirs.get(position); Directory dir = dirs.get(position);
holder.dirName.setText(dir.getName()); holder.dirName.setText(dir.getName());
holder.photoCnt.setText(String.valueOf(dir.getPhotoCnt())); 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; return view;
} }

View file

@ -5,6 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/black" android:background="@android:color/black"
android:choiceMode="multipleChoiceModal"
android:columnWidth="@dimen/dir_tmb_size" android:columnWidth="@dimen/dir_tmb_size"
android:horizontalSpacing="1dp" android:horizontalSpacing="1dp"
android:numColumns="auto_fit" android:numColumns="auto_fit"

View file

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <com.simplemobiletools.gallery.MyRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/tmb_background"> android:foreground="@drawable/selector">
<com.simplemobiletools.gallery.MyImageView <com.simplemobiletools.gallery.MyImageView
android:id="@+id/dir_thumbnail" android:id="@+id/dir_thumbnail"
@ -35,4 +35,4 @@
android:textColor="@android:color/white"/> android:textColor="@android:color/white"/>
</RelativeLayout> </RelativeLayout>
</RelativeLayout> </com.simplemobiletools.gallery.MyRelativeLayout>

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>