make sure only the proper items are selected

This commit is contained in:
tibbi 2016-11-20 21:54:45 +01:00
parent 732fca704c
commit cdb60a4341
1 changed files with 24 additions and 8 deletions

View File

@ -30,9 +30,17 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
companion object { companion object {
var actMode: ActionMode? = null var actMode: ActionMode? = null
val markedItems = HashSet<Int>()
fun toggleItemSelection(itemView: View, select: Boolean) { fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) {
itemView.item_frame.isSelected = select itemView.item_frame.isSelected = select
if (pos == -1)
return
if (select)
markedItems.add(pos)
else
markedItems.remove(pos)
} }
} }
@ -58,11 +66,17 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
return true return true
} }
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu) = true override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean {
val menuItem = menu.findItem(R.id.cab_rename)
menuItem.isVisible = multiSelector.selectedPositions.size <= 1
return true
}
override fun onDestroyActionMode(actionMode: ActionMode?) { override fun onDestroyActionMode(actionMode: ActionMode?) {
super.onDestroyActionMode(actionMode) super.onDestroyActionMode(actionMode)
views.forEach { toggleItemSelection(it, false) } views.forEach { toggleItemSelection(it, false) }
markedItems.clear()
} }
} }
@ -110,14 +124,15 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
views.add(holder.bindView(multiSelectorMode, multiSelector, mItems[position])) views.add(holder.bindView(multiSelectorMode, multiSelector, mItems[position], position))
} }
override fun getItemCount() = mItems.size override fun getItemCount() = mItems.size
class ViewHolder(val activity: SimpleActivity, view: View, val itemClick: (FileDirItem) -> (Unit)) : SwappingHolder(view, MultiSelector()) { class ViewHolder(val activity: SimpleActivity, view: View, val itemClick: (FileDirItem) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, fileDirItem: FileDirItem): View { fun bindView(multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, fileDirItem: FileDirItem, pos: Int): View {
itemView.item_name.text = fileDirItem.name itemView.item_name.text = fileDirItem.name
toggleItemSelection(itemView, markedItems.contains(pos), pos)
if (fileDirItem.isDirectory) { if (fileDirItem.isDirectory) {
Glide.with(activity).load(R.mipmap.directory).diskCacheStrategy(getCacheStrategy(fileDirItem)).centerCrop().crossFade().into(itemView.item_icon) Glide.with(activity).load(R.mipmap.directory).diskCacheStrategy(getCacheStrategy(fileDirItem)).centerCrop().crossFade().into(itemView.item_icon)
@ -127,13 +142,14 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
itemView.item_details.text = fileDirItem.size.formatSize() itemView.item_details.text = fileDirItem.size.formatSize()
} }
itemView.setOnClickListener { viewClicked(multiSelector, fileDirItem) } itemView.setOnClickListener { viewClicked(multiSelector, fileDirItem, pos) }
itemView.setOnLongClickListener { itemView.setOnLongClickListener {
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
multiSelector.setSelected(this, true) multiSelector.setSelected(this, true)
actMode?.title = multiSelector.selectedPositions.size.toString() actMode?.title = multiSelector.selectedPositions.size.toString()
toggleItemSelection(itemView, true) toggleItemSelection(itemView, true, pos)
markedItems.add(pos)
actMode?.invalidate() actMode?.invalidate()
} }
true true
@ -149,11 +165,11 @@ class ItemsAdapter(val activity: SimpleActivity, val mItems: List<FileDirItem>,
return activity.resources.getQuantityString(R.plurals.items, children, children) return activity.resources.getQuantityString(R.plurals.items, children, children)
} }
fun viewClicked(multiSelector: MultiSelector, fileDirItem: FileDirItem) { fun viewClicked(multiSelector: MultiSelector, fileDirItem: FileDirItem, pos: Int) {
if (multiSelector.isSelectable) { if (multiSelector.isSelectable) {
val isSelected = multiSelector.selectedPositions.contains(layoutPosition) val isSelected = multiSelector.selectedPositions.contains(layoutPosition)
multiSelector.setSelected(this, !isSelected) multiSelector.setSelected(this, !isSelected)
toggleItemSelection(itemView, !isSelected) toggleItemSelection(itemView, !isSelected, pos)
val selectedCnt = multiSelector.selectedPositions.size val selectedCnt = multiSelector.selectedPositions.size
if (selectedCnt == 0) { if (selectedCnt == 0) {