From b6bbc8abd7a74d30dae48add313b7a78d1186eb0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 14 May 2017 19:43:51 +0200 Subject: [PATCH] properly update the items after delete --- .../gallery/adapters/DirectoryAdapter.kt | 29 ++++++++++++++----- .../gallery/adapters/MediaAdapter.kt | 28 +++++++++++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index e024a3472..3ae9147f2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -291,10 +291,23 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() + var curIndex = 0 + for (i in 0..itemViews.size() - 1) { + if (itemViews[i] != null) { + newItems.put(curIndex, itemViews[i]) + curIndex++ + } + } + + itemViews = newItems } } @@ -311,7 +324,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, pos: Int, + fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, isPinned: Boolean, listener: DirOperationsListener?): View { itemView.apply { dir_name.text = directory.name @@ -371,15 +384,15 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, files.add(File(medium.path)) removeMedia.add(medium) notifyItemRemoved(it) + itemViews.put(it, null) } media.removeAll(removeMedia) selectedPositions.clear() listener?.deleteFiles(files) + + val newItems = SparseArray() + var curIndex = 0 + for (i in 0..itemViews.size() - 1) { + if (itemViews[i] != null) { + newItems.put(curIndex, itemViews[i]) + curIndex++ + } + } + + itemViews = newItems } } @@ -248,7 +260,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - itemViews.put(position, holder.bindView(activity, multiSelectorMode, multiSelector, media[position], position, listener, displayFilenames)) + itemViews.put(position, holder.bindView(activity, multiSelectorMode, multiSelector, media[position], listener, displayFilenames)) toggleItemSelection(selectedPositions.contains(position), position) holder.itemView.tag = holder } @@ -311,22 +323,22 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, class ViewHolder(val view: View, val adapter: MyAdapterListener, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, - pos: Int, listener: MediaOperationsListener?, displayFilenames: Boolean): View { + listener: MediaOperationsListener?, displayFilenames: Boolean): View { itemView.apply { play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE photo_name.beVisibleIf(displayFilenames) photo_name.text = medium.name activity.loadImage(medium.path, medium_thumbnail) - setOnClickListener { viewClicked(multiSelector, medium, pos) } + setOnClickListener { viewClicked(multiSelector, medium) } setOnLongClickListener { if (listener != null) { if (!multiSelector.isSelectable) { activity.startSupportActionMode(multiSelectorCallback) - adapter.toggleItemSelectionAdapter(true, pos) + adapter.toggleItemSelectionAdapter(true, layoutPosition) } - listener.itemLongClicked(pos) + listener.itemLongClicked(layoutPosition) } true } @@ -336,10 +348,10 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, return itemView } - fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) { + fun viewClicked(multiSelector: MultiSelector, medium: Medium) { if (multiSelector.isSelectable) { - val isSelected = adapter.getSelectedPositions().contains(pos) - adapter.toggleItemSelectionAdapter(!isSelected, pos) + val isSelected = adapter.getSelectedPositions().contains(layoutPosition) + adapter.toggleItemSelectionAdapter(!isSelected, layoutPosition) } else { itemClick(medium) }