Kaynağa Gözat

Dispatch checks/unchecks with payloads, may help #1777

Aidan Follestad 6 yıl önce
ebeveyn
işleme
43ae14a6e3

+ 19 - 0
core/src/main/java/com/afollestad/materialdialogs/internal/list/AdapterPayload.kt

@@ -0,0 +1,19 @@
+/**
+ * Designed and developed by Aidan Follestad (@afollestad)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.afollestad.materialdialogs.internal.list
+
+object CheckPayload
+object UncheckPayload

+ 21 - 2
core/src/main/java/com/afollestad/materialdialogs/internal/list/MultiChoiceDialogAdapter.kt

@@ -82,13 +82,13 @@ internal class MultiChoiceDialogAdapter(
       for (previous in previousSelection) {
         if (!value.contains(previous)) {
           // This value was unselected
-          notifyItemChanged(previous)
+          notifyItemChanged(previous, UncheckPayload)
         }
       }
       for (current in value) {
         if (!previousSelection.contains(current)) {
           // This value was selected
-          notifyItemChanged(current)
+          notifyItemChanged(current, CheckPayload)
         }
       }
     }
@@ -157,6 +157,25 @@ internal class MultiChoiceDialogAdapter(
     }
   }
 
+  override fun onBindViewHolder(
+    holder: MultiChoiceViewHolder,
+    position: Int,
+    payloads: MutableList<Any>
+  ) {
+    when (payloads.firstOrNull()) {
+      CheckPayload -> {
+        holder.controlView.isChecked = true
+        return
+      }
+      UncheckPayload -> {
+        holder.controlView.isChecked = false
+        return
+      }
+    }
+    super.onBindViewHolder(holder, position, payloads)
+    super.onBindViewHolder(holder, position, payloads)
+  }
+
   override fun positiveButtonClicked() {
     if (allowEmptySelection || currentSelection.isNotEmpty()) {
       val selectedItems = items.pullIndices(currentSelection)

+ 20 - 2
core/src/main/java/com/afollestad/materialdialogs/internal/list/SingleChoiceDialogAdapter.kt

@@ -76,8 +76,8 @@ internal class SingleChoiceDialogAdapter(
     set(value) {
       val previousSelection = field
       field = value
-      notifyItemChanged(previousSelection)
-      notifyItemChanged(value)
+      notifyItemChanged(previousSelection, UncheckPayload)
+      notifyItemChanged(value, CheckPayload)
     }
   private var disabledIndices: IntArray = disabledItems ?: IntArray(0)
 
@@ -136,6 +136,24 @@ internal class SingleChoiceDialogAdapter(
     }
   }
 
+  override fun onBindViewHolder(
+    holder: SingleChoiceViewHolder,
+    position: Int,
+    payloads: MutableList<Any>
+  ) {
+    when (payloads.firstOrNull()) {
+      CheckPayload -> {
+        holder.controlView.isChecked = true
+        return
+      }
+      UncheckPayload -> {
+        holder.controlView.isChecked = false
+        return
+      }
+    }
+    super.onBindViewHolder(holder, position, payloads)
+  }
+
   override fun positiveButtonClicked() {
     if (currentSelection > -1) {
       selection?.invoke(dialog, currentSelection, items[currentSelection])