fix #73, add drag-selection to recycler view

This commit is contained in:
tibbi 2017-06-20 23:32:07 +02:00
parent c079bbb699
commit b27c9bb491
2 changed files with 69 additions and 7 deletions

View File

@ -247,6 +247,45 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
override fun getItemCount() = mItems.size
fun selectItem(pos: Int) {
toggleItemSelection(true, pos)
}
fun selectRange(from: Int, to: Int, min: Int, max: Int) {
if (from == to) {
(min..max).filter { it != from }
.forEach { toggleItemSelection(false, it) }
return
}
if (to < from) {
for (i in to..from)
toggleItemSelection(true, i)
if (min > -1 && min < to) {
(min..to - 1).filter { it != from }
.forEach { toggleItemSelection(false, it) }
}
if (max > -1) {
for (i in from + 1..max)
toggleItemSelection(false, i)
}
} else {
for (i in from..to)
toggleItemSelection(true, i)
if (max > -1 && max > to) {
(to + 1..max).filter { it != from }
.forEach { toggleItemSelection(false, it) }
}
if (min > -1) {
for (i in min..from - 1)
toggleItemSelection(false, i)
}
}
}
class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
val multiSelector: MultiSelector, val listener: ItemOperationsListener?, val itemClick: (FileDirItem) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(fileDirItem: FileDirItem, fileDrawable: Drawable, folderDrawable: Drawable, textColor: Int): View {

View File

@ -13,6 +13,7 @@ import android.view.ViewGroup
import android.webkit.MimeTypeMap
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyScalableRecyclerView
import com.simplemobiletools.commons.views.RecyclerViewDivider
import com.simplemobiletools.filemanager.PATH
import com.simplemobiletools.filemanager.R
@ -86,17 +87,17 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
mItems = newItems
val adapter = ItemsAdapter(activity as SimpleActivity, mItems, this@ItemsFragment) {
itemClicked(it)
}
val currAdapter = items_list.adapter
if (currAdapter == null) {
items_list.apply {
this.adapter = adapter
this.adapter = ItemsAdapter(activity as SimpleActivity, mItems, this@ItemsFragment) {
itemClicked(it)
}
addItemDecoration(RecyclerViewDivider(context))
}
items_list.isDragSelectionEnabled = true
items_fastscroller.setViews(items_list, items_swipe_refresh)
setupRecyclerViewListener()
} else {
val state = (items_list.layoutManager as LinearLayoutManager).onSaveInstanceState()
(currAdapter as ItemsAdapter).updateItems(mItems)
@ -109,7 +110,29 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
}
}
fun getRecyclerLayoutManager() = (fragmentView.items_list.layoutManager as LinearLayoutManager)
private fun getRecyclerLayoutManager() = (fragmentView.items_list.layoutManager as LinearLayoutManager)
private fun setupRecyclerViewListener() {
fragmentView.items_list.listener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener {
override fun zoomIn() {
}
override fun zoomOut() {
}
override fun selectItem(position: Int) {
getRecyclerAdapter().selectItem(position)
}
override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) {
getRecyclerAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached)
}
}
}
private fun getRecyclerAdapter() = (items_list.adapter as ItemsAdapter)
fun getScrollState() = getRecyclerLayoutManager().onSaveInstanceState()
@ -217,7 +240,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
}
override fun itemLongClicked(position: Int) {
items_list.setDragSelectActive(position)
}
interface ItemInteractionListener {