multi-select-fix-rotate-899

This commit is contained in:
David Wiesner 2016-06-06 14:42:07 +02:00 committed by jabarros
parent 7631bc24d3
commit a3fae49fbf
2 changed files with 91 additions and 35 deletions

View file

@ -35,6 +35,7 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -61,6 +62,8 @@ import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.MimetypeIconUtil;
import java.util.Vector;
import java.util.HashSet;
import java.util.LinkedHashSet;
/**
@ -69,6 +72,8 @@ import java.util.Vector;
*/
public class FileListListAdapter extends BaseAdapter implements ListAdapter {
private static final String SELECTION_KEY = "multiFileSelectionsKey";
private Context mContext;
private OCFile mFile = null;
private Vector<OCFile> mFiles = null;
@ -82,7 +87,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM };
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
private HashSet<Long> mSelection = new LinkedHashSet<Long>();
public FileListListAdapter(
boolean justFolders,
@ -382,7 +387,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
}
}
if (mSelection.get(position) != null) {
if (mSelection.contains(getItemId(position))) {
view.setBackgroundColor(mContext.getResources().getColor(R.color.selected_item_background));
} else {
view.setBackgroundColor(Color.WHITE);
@ -471,6 +476,59 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
mGridMode = gridMode;
}
// TODO Tobi: all methods needed?
public void setNewSelection(int position, boolean checked) {
if(checked){
mSelection.add(getItemId(position));
notifyDataSetChanged();
} else {
removeSelection(position);
}
}
public void removeSelection(int position) {
mSelection.remove(getItemId(position));
notifyDataSetChanged();
}
public void removeSelection(){
mSelection.clear();
notifyDataSetChanged();
}
public ArrayList<OCFile> getCheckedItems() {
ArrayList<OCFile> files = new ArrayList<OCFile>();
if (mFiles != null && mFiles.size() != 0){
for(OCFile file: mFiles){
if(mSelection.contains(file.getFileId())){
files.add(file);
}
}
}
return files;
}
public void restoreSelectionState(Bundle savedInstanceState){
if (savedInstanceState == null) {
return;
}
long[] selectionState = savedInstanceState.getLongArray(SELECTION_KEY);
mSelection.clear();
if(selectionState != null) {
for (long id : selectionState) {
mSelection.add(id);
}
}
}
public void saveSelectionState(Bundle outState) {
long[] selectionStatePrimitive = new long[mSelection.size()];
int i = 0;
for (Long id : mSelection) {
selectionStatePrimitive[i++] = id;
}
outState.putLongArray(SELECTION_KEY, selectionStatePrimitive);
}
public boolean isGridMode() {
return mGridMode;
}

View file

@ -31,7 +31,6 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -109,6 +108,7 @@ public class OCFileListFragment extends ExtendedListFragment {
private boolean hideFab = true;
private boolean miniFabClicked = false;
private ActionMode mActiveActionMode;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -186,6 +186,7 @@ public class OCFileListFragment extends ExtendedListFragment {
getActivity(),
mContainerActivity
);
mAdapter.restoreSelectionState(savedInstanceState);
setListAdapter(mAdapter);
registerLongClickListener();
@ -348,27 +349,18 @@ public class OCFileListFragment extends ExtendedListFragment {
setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
private Menu menu;
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
mAdapter.setNewSelection(position, checked);
} else {
mAdapter.removeSelection(position);
}
updateActionsMenu(mode);
mode.invalidate();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mActiveActionMode = mode;
createContextActionBar(menu);
this.menu = menu;
updateActionsMenu(mode);
mode.invalidate();
//set gray color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@ -381,7 +373,8 @@ public class OCFileListFragment extends ExtendedListFragment {
return true;
}
private void updateActionsMenu(ActionMode mode) {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
final int checkedCount = getListView().getCheckedItemCount();
mode.setTitle(checkedCount + " selected");
@ -394,16 +387,11 @@ public class OCFileListFragment extends ExtendedListFragment {
targetFiles,
mContainerActivity.getStorageManager().getAccount(),
mContainerActivity,
getActivity()
);
getActivity());
mf.filter(menu);
}
}
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
return true;
}
@Override
@ -414,6 +402,7 @@ public class OCFileListFragment extends ExtendedListFragment {
@Override
public void onDestroyActionMode(ActionMode mode) {
mAdapter.removeSelection();
mActiveActionMode = null;
// reset to primary dark color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@ -425,13 +414,14 @@ public class OCFileListFragment extends ExtendedListFragment {
setFabEnabled(true);
}
}
});
}
// TODO Tobi needed?
private void showFileAction(int fileIndex) {
Bundle args = getArguments();
PopupMenu pm = new PopupMenu(getActivity(),null);
PopupMenu pm = new PopupMenu(getActivity(), null);
Menu menu = pm.getMenu();
boolean allowContextualActions =
@ -455,7 +445,7 @@ public class OCFileListFragment extends ExtendedListFragment {
/// TODO break this direct dependency on FileDisplayActivity... if possible
MenuItem item = menu.findItem(R.id.action_open_file_with);
FileFragment frag = ((FileDisplayActivity)getActivity()).getSecondFragment();
FileFragment frag = ((FileDisplayActivity) getActivity()).getSecondFragment();
if (frag != null && frag instanceof FileDetailFragment &&
frag.getFile().getFileId() == targetFile.getFileId()) {
item = menu.findItem(R.id.action_see_details);
@ -479,6 +469,7 @@ public class OCFileListFragment extends ExtendedListFragment {
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(KEY_FILE, mFile);
mAdapter.saveSelectionState(outState);
}
@Override
@ -816,6 +807,13 @@ public class OCFileListFragment extends ExtendedListFragment {
switchToListView();
}
}
invalidateActionMode();
}
private void invalidateActionMode() {
if(mActiveActionMode != null){
mActiveActionMode.invalidate();
}
}
private String generateFooterText(int filesCount, int foldersCount) {