1
0
Эх сурвалжийг харах

Added clearSelectedIndices() method, resolves https://github.com/afollestad/material-dialogs/issues/604

Aidan Follestad 9 жил өмнө
parent
commit
6b2d0986d6

+ 15 - 0
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -1502,6 +1502,21 @@ public class MaterialDialog extends DialogBase implements
         }
     }
 
+    /**
+     * Clears all selected checkboxes from multi choice list dialogs.
+     */
+    public void clearSelectedIndices() {
+        if (selectedIndicesList == null)
+            throw new IllegalStateException("You can only use clearSelectedIndicies() with multi choice list dialogs.");
+        mBuilder.selectedIndices = null;
+        selectedIndicesList.clear();
+        if (mBuilder.adapter != null && mBuilder.adapter instanceof MaterialDialogAdapter) {
+            ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
+        } else {
+            throw new IllegalStateException("You can only use clearSelectedIndicies() with the default adapter implementation.");
+        }
+    }
+
     @Override
     public final void onShow(DialogInterface dialog) {
         if (input != null) {

+ 8 - 0
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -422,8 +422,16 @@ public class MainActivity extends AppCompatActivity implements
                         return true; // allow selection
                     }
                 })
+                .callback(new MaterialDialog.ButtonCallback() {
+                    @Override
+                    public void onNeutral(MaterialDialog dialog) {
+                        dialog.clearSelectedIndices();
+                    }
+                })
                 .alwaysCallMultiChoiceCallback()
                 .positiveText(R.string.choose)
+                .autoDismiss(false)
+                .neutralText(R.string.clear_selection)
                 .show();
     }
 

+ 1 - 0
sample/src/main/res/values/strings.xml

@@ -162,5 +162,6 @@
     <string name="material_dialog_pref_title">Dialog Preference</string>
     <string name="material_dialog_pref_summary">This is an example of a basic dialog preference that automatically uses Material Dialogs to show the dialog.</string>
     <string name="input_content_custominvalidation">Do not type hello, or else!</string>
+    <string name="clear_selection">Clear</string>
 
 </resources>