change the way we get layout position in adapters

This commit is contained in:
tibbi 2017-09-17 10:50:23 +02:00
parent 52f1cb1c42
commit ae6ea422ac
2 changed files with 24 additions and 24 deletions

View file

@ -336,7 +336,7 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList<Direc
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val dir = dirs[position] val dir = dirs[position]
itemViews.put(position, holder.bindView(dir, pinnedFolders.contains(dir.path), scrollVertically)) itemViews.put(position, holder.bindView(dir, pinnedFolders.contains(dir.path), scrollVertically, position))
toggleItemSelection(selectedPositions.contains(position), position) toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -369,7 +369,7 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList<Direc
toggleItemSelection(true, i) toggleItemSelection(true, i)
if (min > -1 && min < to) { if (min > -1 && min < to) {
(min..to - 1).filter { it != from } (min until to).filter { it != from }
.forEach { toggleItemSelection(false, it) } .forEach { toggleItemSelection(false, it) }
} }
if (max > -1) { if (max > -1) {
@ -386,7 +386,7 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList<Direc
} }
if (min > -1) { if (min > -1) {
for (i in min..from - 1) for (i in min until from)
toggleItemSelection(false, i) toggleItemSelection(false, i)
} }
} }
@ -395,7 +395,7 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList<Direc
class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback, class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
val multiSelector: MultiSelector, val listener: DirOperationsListener?, val isPickIntent: Boolean, val itemClick: (Directory) -> (Unit)) : val multiSelector: MultiSelector, val listener: DirOperationsListener?, val isPickIntent: Boolean, val itemClick: (Directory) -> (Unit)) :
SwappingHolder(view, MultiSelector()) { SwappingHolder(view, MultiSelector()) {
fun bindView(directory: Directory, isPinned: Boolean, scrollVertically: Boolean): View { fun bindView(directory: Directory, isPinned: Boolean, scrollVertically: Boolean, position: Int): View {
itemView.apply { itemView.apply {
dir_name.text = directory.name dir_name.text = directory.name
photo_cnt.text = directory.mediaCnt.toString() photo_cnt.text = directory.mediaCnt.toString()
@ -403,29 +403,29 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList<Direc
dir_pin.beVisibleIf(isPinned) dir_pin.beVisibleIf(isPinned)
dir_sd_card.beVisibleIf(activity.isPathOnSD(directory.path)) dir_sd_card.beVisibleIf(activity.isPathOnSD(directory.path))
setOnClickListener { viewClicked(directory) } setOnClickListener { viewClicked(directory, position) }
setOnLongClickListener { if (isPickIntent) viewClicked(directory) else viewLongClicked(); true } setOnLongClickListener { if (isPickIntent) viewClicked(directory, position) else viewLongClicked(position); true }
} }
return itemView return itemView
} }
fun viewClicked(directory: Directory) { private fun viewClicked(directory: Directory, position: Int) {
if (multiSelector.isSelectable) { if (multiSelector.isSelectable) {
val isSelected = adapterListener.getSelectedPositions().contains(layoutPosition) val isSelected = adapterListener.getSelectedPositions().contains(position)
adapterListener.toggleItemSelectionAdapter(!isSelected, layoutPosition) adapterListener.toggleItemSelectionAdapter(!isSelected, position)
} else { } else {
itemClick(directory) itemClick(directory)
} }
} }
fun viewLongClicked() { private fun viewLongClicked(position: Int) {
if (listener != null) { if (listener != null) {
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
adapterListener.toggleItemSelectionAdapter(true, layoutPosition) adapterListener.toggleItemSelectionAdapter(true, position)
} }
listener.itemLongClicked(layoutPosition) listener.itemLongClicked(position)
} }
} }

View file

@ -251,7 +251,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
itemViews.put(position, holder.bindView(media[position], displayFilenames, scrollVertically)) itemViews.put(position, holder.bindView(media[position], displayFilenames, scrollVertically, position))
toggleItemSelection(selectedPositions.contains(position), position) toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -289,7 +289,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
toggleItemSelection(true, i) toggleItemSelection(true, i)
if (min > -1 && min < to) { if (min > -1 && min < to) {
(min..to - 1).filter { it != from } (min until to).filter { it != from }
.forEach { toggleItemSelection(false, it) } .forEach { toggleItemSelection(false, it) }
} }
if (max > -1) { if (max > -1) {
@ -306,7 +306,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
if (min > -1) { if (min > -1) {
for (i in min..from - 1) for (i in min until from)
toggleItemSelection(false, i) toggleItemSelection(false, i)
} }
} }
@ -315,36 +315,36 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback, class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
val multiSelector: MultiSelector, val listener: MediaOperationsListener?, val isPickIntent: Boolean, val itemClick: (Medium) -> (Unit)) : val multiSelector: MultiSelector, val listener: MediaOperationsListener?, val isPickIntent: Boolean, val itemClick: (Medium) -> (Unit)) :
SwappingHolder(view, MultiSelector()) { SwappingHolder(view, MultiSelector()) {
fun bindView(medium: Medium, displayFilenames: Boolean, scrollVertically: Boolean): View { fun bindView(medium: Medium, displayFilenames: Boolean, scrollVertically: Boolean, position: Int): View {
itemView.apply { itemView.apply {
play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE
photo_name.beVisibleIf(displayFilenames) photo_name.beVisibleIf(displayFilenames)
photo_name.text = medium.name photo_name.text = medium.name
activity.loadImage(medium.path, medium_thumbnail, scrollVertically) activity.loadImage(medium.path, medium_thumbnail, scrollVertically)
setOnClickListener { viewClicked(medium) } setOnClickListener { viewClicked(medium, position) }
setOnLongClickListener { if (isPickIntent) viewClicked(medium) else viewLongClicked(); true } setOnLongClickListener { if (isPickIntent) viewClicked(medium, position) else viewLongClicked(position); true }
} }
return itemView return itemView
} }
fun viewClicked(medium: Medium) { private fun viewClicked(medium: Medium, position: Int) {
if (multiSelector.isSelectable) { if (multiSelector.isSelectable) {
val isSelected = adapterListener.getSelectedPositions().contains(layoutPosition) val isSelected = adapterListener.getSelectedPositions().contains(position)
adapterListener.toggleItemSelectionAdapter(!isSelected, layoutPosition) adapterListener.toggleItemSelectionAdapter(!isSelected, position)
} else { } else {
itemClick(medium) itemClick(medium)
} }
} }
fun viewLongClicked() { private fun viewLongClicked(position: Int) {
if (listener != null) { if (listener != null) {
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
adapterListener.toggleItemSelectionAdapter(true, layoutPosition) adapterListener.toggleItemSelectionAdapter(true, position)
} }
listener.itemLongClicked(layoutPosition) listener.itemLongClicked(position)
} }
} }