소스 검색

Add support for enabling always single choice callback, to match multichoice

Henri Sweers 10 년 전
부모
커밋
fb70358942
1개의 변경된 파일17개의 추가작업 그리고 0개의 파일을 삭제
  1. 17 0
      library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

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

@@ -70,6 +70,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
     protected View negativeButton;
     protected boolean isStacked;
     protected boolean alwaysCallMultiChoiceCallback;
+    protected boolean alwaysCallSingleChoiceCallback;
     protected final int defaultItemColor;
     protected ListType listType;
     protected List<Integer> selectedIndicesList;
@@ -201,6 +202,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
                 // Determine list type
                 if (mBuilder.listCallbackSingle != null) {
                     listType = ListType.SINGLE;
+                    alwaysCallSingleChoiceCallback = builder.alwaysCallSingleChoiceCallback;
                 } else if (mBuilder.listCallbackMulti != null) {
                     listType = ListType.MULTI;
                     if (mBuilder.selectedIndices != null) {
@@ -786,6 +788,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
                     if (mBuilder.autoDismiss && mBuilder.positiveText == null) {
                         dismiss();
                         sendSingleChoiceCallback(v);
+                    } else if (alwaysCallSingleChoiceCallback) {
+                        sendSingleChoiceCallback(v);
                     }
                 } else if (mBuilder.listCallbackMulti != null) {
                     CheckBox cb = (CheckBox) ((LinearLayout) v).getChildAt(0);
@@ -824,6 +828,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         protected ListCallback listCallbackSingle;
         protected ListCallbackMulti listCallbackMulti;
         protected boolean alwaysCallMultiChoiceCallback = false;
+        protected boolean alwaysCallSingleChoiceCallback = false;
         protected Theme theme = Theme.LIGHT;
         protected boolean cancelable = true;
         protected float contentLineSpacingMultiplier = 1.3f;
@@ -1018,6 +1023,18 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             return this;
         }
 
+        /**
+         * By default, the single choice callback is only called when the user clicks the positive button
+         * or if there are no buttons. Call this to force it to always call on item clicks even if the
+         * positive button exists.
+         *
+         * @return The Builder instance so you can chain calls to it.
+         */
+        public Builder alwaysCallSingleChoiceCallback() {
+            this.alwaysCallSingleChoiceCallback = true;
+            return this;
+        }
+
         /**
          * Pass null for the selected indices to leave all options unselected initially. Otherwise pass
          * an array of indices that will be selected initially.