use integers for storing selected recyclerview item keys

This commit is contained in:
tibbi 2018-10-14 18:50:06 +02:00
parent 19d9f20b89
commit 3acb283fa3
3 changed files with 19 additions and 13 deletions

View File

@ -42,7 +42,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.0.16' implementation 'com.simplemobiletools:commons:5.0.18'
implementation files('../libs/RootTools.jar') implementation files('../libs/RootTools.jar')
} }

View File

@ -99,9 +99,9 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
override fun getIsItemSelectable(position: Int) = true override fun getIsItemSelectable(position: Int) = true
override fun getItemSelectionKey(position: Int) = fileDirItems.getOrNull(position)?.path override fun getItemSelectionKey(position: Int) = fileDirItems.getOrNull(position)?.path?.hashCode()
override fun getItemKeyPosition(key: String) = fileDirItems.indexOfFirst { it.path == key } override fun getItemKeyPosition(key: Int) = fileDirItems.indexOfFirst { it.path.hashCode() == key }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.list_item, parent) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.list_item, parent)
@ -115,7 +115,7 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
override fun getItemCount() = fileDirItems.size override fun getItemCount() = fileDirItems.size
private fun getItemWithKey(key: String): FileDirItem? = fileDirItems.firstOrNull { it.path == key } private fun getItemWithKey(key: Int): FileDirItem? = fileDirItems.firstOrNull { it.path.hashCode() == key }
fun initDrawables() { fun initDrawables() {
folderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_folder, textColor) folderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_folder, textColor)
@ -493,9 +493,9 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
val files = ArrayList<FileDirItem>(selectedKeys.size) val files = ArrayList<FileDirItem>(selectedKeys.size)
val positions = ArrayList<Int>() val positions = ArrayList<Int>()
selectedKeys.forEach { selectedKeys.forEach {
activity.config.removeFavorite(it) activity.config.removeFavorite(getItemWithKey(it)?.path ?: "")
val key = it val key = it
val position = fileDirItems.indexOfFirst { it.path == key } val position = fileDirItems.indexOfFirst { it.path.hashCode() == key }
if (position != -1) { if (position != -1) {
positions.add(position) positions.add(position)
files.add(fileDirItems[position]) files.add(fileDirItems[position])
@ -545,7 +545,7 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
} }
private fun setupView(view: View, fileDirItem: FileDirItem) { private fun setupView(view: View, fileDirItem: FileDirItem) {
val isSelected = isKeySelected(fileDirItem.path) val isSelected = isKeySelected(fileDirItem.path.hashCode())
view.apply { view.apply {
item_frame.isSelected = isSelected item_frame.isSelected = isSelected
val fileName = fileDirItem.name val fileName = fileDirItem.name

View File

@ -33,9 +33,9 @@ class ManageFavoritesAdapter(activity: BaseSimpleActivity, var favorites: ArrayL
override fun getIsItemSelectable(position: Int) = true override fun getIsItemSelectable(position: Int) = true
override fun getItemSelectionKey(position: Int) = favorites.getOrNull(position) override fun getItemSelectionKey(position: Int) = favorites.getOrNull(position)?.hashCode()
override fun getItemKeyPosition(key: String) = favorites.indexOfFirst { it == key } override fun getItemKeyPosition(key: Int) = favorites.indexOfFirst { it.hashCode() == key }
override fun prepareActionMode(menu: Menu) {} override fun prepareActionMode(menu: Menu) {}
@ -44,7 +44,7 @@ class ManageFavoritesAdapter(activity: BaseSimpleActivity, var favorites: ArrayL
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val favorite = favorites[position] val favorite = favorites[position]
holder.bindView(favorite, true, true) { itemView, layoutPosition -> holder.bindView(favorite, true, true) { itemView, layoutPosition ->
setupView(itemView, favorite, isKeySelected(favorite)) setupView(itemView, favorite, isKeySelected(favorite.hashCode()))
} }
bindViewHolder(holder) bindViewHolder(holder)
} }
@ -67,11 +67,15 @@ class ManageFavoritesAdapter(activity: BaseSimpleActivity, var favorites: ArrayL
val positions = java.util.ArrayList<Int>() val positions = java.util.ArrayList<Int>()
selectedKeys.forEach { selectedKeys.forEach {
val key = it val key = it
val position = favorites.indexOfFirst { it == key } val position = favorites.indexOfFirst { it.hashCode() == key }
if (position != -1) { if (position != -1) {
positions.add(position) positions.add(position)
removeFavorites.add(key)
config.removeFavorite(key) val favorite = getItemWithKey(key)
if (favorite != null) {
removeFavorites.add(favorite)
config.removeFavorite(favorite)
}
} }
} }
@ -83,4 +87,6 @@ class ManageFavoritesAdapter(activity: BaseSimpleActivity, var favorites: ArrayL
listener?.refreshItems() listener?.refreshItems()
} }
} }
private fun getItemWithKey(key: Int): String? = favorites.firstOrNull { it.hashCode() == key }
} }