[Android] MultiSelectHelper tool

Geoffrey Métais git at videolan.org
Thu Sep 6 16:16:21 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Sep  6 15:11:55 2018 +0200| [e62f15f476e8bf7b220c363f9839b5f57ef036d0] | committer: Geoffrey Métais

MultiSelectHelper tool

> https://code.videolan.org/videolan/vlc-android/commit/e62f15f476e8bf7b220c363f9839b5f57ef036d0
---

 .../java/org/videolan/tools/MultiSelectHelper.kt   | 41 ++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt b/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt
new file mode 100644
index 000000000..fe3567631
--- /dev/null
+++ b/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt
@@ -0,0 +1,41 @@
+package org.videolan.tools
+
+import android.support.annotation.MainThread
+import android.util.SparseBooleanArray
+
+
+class MultiSelectHelper<T>(val adapter: MultiSelectAdapter<T>, private val payloadvalue : Any = 0) {
+
+    private val selection = SparseBooleanArray()
+
+    fun getSelection(): List<T> {
+        val list = ArrayList<T>(selection.size())
+        for (i in 0 until selection.size()) list.add(adapter.getItem(selection.keyAt(i)))
+        return list
+    }
+
+    @MainThread
+    fun getSelectionCount() = selection.size()
+
+    fun toggleSelection(position: Int) {
+        if (isSelected(position)) selection.delete(position)
+        else selection.append(position, true)
+        adapter.notifyItemChanged(position, payloadvalue)
+    }
+
+    fun clearSelection() {
+        if (selection.size() == 0) return
+        val start = selection.keyAt(0)
+        val count = selection.keyAt(selection.size() - 1) - start + 1
+        selection.clear()
+        adapter.notifyItemRangeChanged(start, count, payloadvalue)
+    }
+
+    fun isSelected(position: Int) = selection.get(position, false)
+}
+
+interface MultiSelectAdapter<T> {
+    fun getItem(position: Int) : T
+    fun notifyItemChanged(start: Int, payload: Any?)
+    fun notifyItemRangeChanged(start: Int, count: Int, payload: Any?)
+}
\ No newline at end of file



More information about the Android mailing list