瀏覽代碼

Merge pull request #128 from hzsweers/master

Switch to ButtonCallback to allow a-la-carte callback method implementation and convenience getters for currently selected indices
Aidan Follestad 10 年之前
父節點
當前提交
d5978c2d6b

+ 6 - 4
README.md

@@ -178,18 +178,20 @@ new MaterialDialog.Builder(this)
 
 ### Callbacks
 
-To know when the user selects an action button, you set a callback. There's three variations of the callback for the action buttons:
+To know when the user selects an action button, you set a callback. To do this, use the `ButtonCallback`
+class and override its `onPositive()`, `onNegative()`, or `onNeutral()` methods as needed. The advantage
+to this is that you can override button functionality *À la carte*, so no need to stub empty methods.
 
 ```java
 new MaterialDialog.Builder(this)
-        .callback(new MaterialDialog.SimpleCallback() {
+        .callback(new MaterialDialog.ButtonCallback() {
             @Override
             public void onPositive(MaterialDialog dialog) {
             }
         });
 
 new MaterialDialog.Builder(this)
-        .callback(new MaterialDialog.Callback() {
+        .callback(new MaterialDialog.ButtonCallback() {
             @Override
             public void onPositive(MaterialDialog dialog) {
             }
@@ -200,7 +202,7 @@ new MaterialDialog.Builder(this)
         });
 
 new MaterialDialog.Builder(this)
-        .callback(new MaterialDialog.FullCallback() {
+        .callback(new MaterialDialog.ButtonCallback() {
             @Override
             public void onPositive(MaterialDialog dialog) {
             }

+ 70 - 13
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -67,7 +67,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
     private int positiveColor;
     private int negativeColor;
     private int neutralColor;
-    private SimpleCallback callback;
+    private ButtonCallback callback;
     private ListCallback listCallback;
     private ListCallback listCallbackSingle;
     private ListCallbackMulti listCallbackMulti;
@@ -575,14 +575,14 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
                 if (autoDismiss) dismiss();
                 break;
             case NEGATIVE:
-                if (callback != null && callback instanceof Callback) {
-                    ((Callback) callback).onNegative(this);
+                if (callback != null) {
+                    callback.onNegative(this);
                 }
                 if (autoDismiss) dismiss();
                 break;
             case NEUTRAL:
-                if (callback != null && callback instanceof FullCallback) {
-                    ((FullCallback) callback).onNeutral(this);
+                if (callback != null) {
+                    callback.onNeutral(this);
                 }
                 if (autoDismiss) dismiss();
                 break;
@@ -641,7 +641,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         protected int positiveColor;
         protected int negativeColor;
         protected int neutralColor;
-        protected SimpleCallback callback;
+        protected ButtonCallback callback;
         protected ListCallback listCallback;
         protected ListCallback listCallbackSingle;
         protected ListCallbackMulti listCallbackMulti;
@@ -900,7 +900,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
             return this;
         }
 
-        public Builder callback(SimpleCallback callback) {
+        public Builder callback(ButtonCallback callback) {
             this.callback = callback;
             return this;
         }
@@ -1162,6 +1162,33 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         return listView;
     }
 
+    /**
+     * Convenience method for getting the currently selected index of a single choice list
+     *
+     * @return Currently selected index of a single choice list, or -1 if not showing a single choice list
+     */
+    public int getSelectedIndex() {
+        if (listCallbackSingle != null) {
+            return selectedIndex;
+        } else {
+            return -1;
+        }
+    }
+
+    /**
+     * Convenience method for getting the currently selected indices of a multi choice list
+     *
+     * @return Currently selected index of a multi choice list, or null if not showing a multi choice list
+     */
+    @Nullable
+    public Integer[] getSelectedIndices() {
+        if (listCallbackMulti != null) {
+            return selectedIndices;
+        } else {
+            return null;
+        }
+    }
+
     private class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
 
         final int itemColor;
@@ -1236,15 +1263,45 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text);
     }
 
-    public static interface SimpleCallback {
-        void onPositive(MaterialDialog dialog);
+    /**
+     * @deprecated Use the new {@link com.afollestad.materialdialogs.MaterialDialog.ButtonCallback}
+     */
+    public abstract static class SimpleCallback extends ButtonCallback {
+        @Override
+        public abstract void onPositive(MaterialDialog dialog);
     }
 
-    public static interface Callback extends SimpleCallback {
-        void onNegative(MaterialDialog dialog);
+    /**
+     * @deprecated Use the new {@link com.afollestad.materialdialogs.MaterialDialog.ButtonCallback}
+     */
+    public abstract static class Callback extends SimpleCallback {
+        @Override
+        public abstract void onNegative(MaterialDialog dialog);
+    }
+
+    /**
+     * @deprecated Use the new {@link com.afollestad.materialdialogs.MaterialDialog.ButtonCallback}
+     */
+    public abstract static class FullCallback extends Callback {
+        @Override
+        public abstract void onNeutral(MaterialDialog dialog);
     }
 
-    public static interface FullCallback extends Callback {
-        void onNeutral(MaterialDialog dialog);
+    /**
+     * Override these as needed, so no needing to sub empty methods from an interface
+     */
+    public static class ButtonCallback {
+
+        public void onPositive(MaterialDialog dialog) {
+
+        }
+
+        public void onNegative(MaterialDialog dialog) {
+
+        }
+
+        public void onNeutral(MaterialDialog dialog) {
+
+        }
     }
 }

+ 1 - 1
library/src/main/java/com/afollestad/materialdialogs/MaterialDialogCompat.java

@@ -261,7 +261,7 @@ public class MaterialDialogCompat {
 
         private void addButtonsCallback() {
             if (positiveDialogListener != null || negativeDialogListener != null) {
-                builder.callback(new MaterialDialog.FullCallback() {
+                builder.callback(new MaterialDialog.ButtonCallback() {
                     @Override
                     public void onNeutral(MaterialDialog dialog) {
                         if (neutralDialogListener != null) {

+ 14 - 12
sample/src/main/java/com/afollestad/materialdialogssample/FolderSelectorDialog.java

@@ -18,12 +18,23 @@ import java.util.List;
 /**
  * @author Aidan Follestad (afollestad)
  */
-public class FolderSelectorDialog extends DialogFragment implements MaterialDialog.ListCallback, MaterialDialog.Callback {
+public class FolderSelectorDialog extends DialogFragment implements MaterialDialog.ListCallback {
 
     File parentFolder;
     File[] parentContents;
     boolean canGoUp = true;
     Callback mCallback;
+    MaterialDialog.ButtonCallback mButtonCallback = new MaterialDialog.ButtonCallback() {
+        @Override
+        public void onPositive(MaterialDialog materialDialog) {
+            materialDialog.dismiss();
+            mCallback.onFolderSelection(parentFolder);
+        }
+        @Override
+        public void onNegative(MaterialDialog materialDialog) {
+            materialDialog.dismiss();
+        }
+    };
 
     public static interface Callback {
         void onFolderSelection(File folder);
@@ -58,7 +69,7 @@ public class FolderSelectorDialog extends DialogFragment implements MaterialDial
                 .title(parentFolder.getAbsolutePath())
                 .items(getContentsArray())
                 .itemsCallback(this)
-                .callback(this)
+                .callback(mButtonCallback)
                 .autoDismiss(false)
                 .positiveText(R.string.choose)
                 .negativeText(android.R.string.cancel)
@@ -80,21 +91,12 @@ public class FolderSelectorDialog extends DialogFragment implements MaterialDial
         dialog.setItems(getContentsArray());
     }
 
-    @Override
-    public void onPositive(MaterialDialog materialDialog) {
-        materialDialog.dismiss();
-        mCallback.onFolderSelection(parentFolder);
-    }
-
     public void show(Activity context, Callback callback) {
         mCallback = callback;
         show(context.getFragmentManager(), "FOLDER_SELECTOR");
     }
 
-    @Override
-    public void onNegative(MaterialDialog materialDialog) {
-        materialDialog.dismiss();
-    }
+
 
     public static class FolderSorter implements Comparator<File> {
         @Override

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

@@ -224,7 +224,7 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
                 .positiveText(R.string.agree)
                 .negativeText(R.string.disagree)
                 .neutralText(R.string.more_info)
-                .callback(new MaterialDialog.FullCallback() {
+                .callback(new MaterialDialog.ButtonCallback() {
                     @Override
                     public void onPositive(MaterialDialog dialog) {
                         Toast.makeText(getApplicationContext(), "Positive!", Toast.LENGTH_SHORT).show();
@@ -331,7 +331,7 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
                         Toast.makeText(MainActivity.this, "Clicked " + text, Toast.LENGTH_SHORT).show();
                     }
                 })
-                .callback(new MaterialDialog.FullCallback() {
+                .callback(new MaterialDialog.ButtonCallback() {
                     @Override
                     public void onNeutral(MaterialDialog dialog) {
                         Toast.makeText(MainActivity.this, "Maybe", Toast.LENGTH_SHORT).show();
@@ -385,7 +385,7 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
                 .customView(R.layout.dialog_customview)
                 .positiveText(R.string.connect)
                 .negativeText(android.R.string.cancel)
-                .callback(new MaterialDialog.Callback() {
+                .callback(new MaterialDialog.ButtonCallback() {
                     @Override
                     public void onPositive(MaterialDialog dialog) {
                         Toast.makeText(getApplicationContext(), "Password: " + passwordInput.getText().toString(), Toast.LENGTH_SHORT).show();