Sfoglia il codice sorgente

Fixed list item selectors, made selectors customizable via methods and global theming.

Aidan Follestad 10 anni fa
parent
commit
bbe879179e

+ 10 - 0
README.md

@@ -439,6 +439,16 @@ or operating system. This behavior can be overridden in your Activity themes:
     -->
     <item name="md_divider_color">#E91E63</item>
 
+    <!--
+        This overrides the selector used on list items and stacked action buttons
+    -->
+    <item name="md_selector">@drawable/selector</item>
+
+    <!--
+        This overrides the selector used on action buttons
+    -->
+    <item name="md_btn_selector">@drawable/selector</item>
+
 </style>
 ```
 

+ 46 - 12
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -559,7 +559,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
 
             // We got here, so now we can safely check
             isRecyclerView = RecyclerUtil.isRecyclerView(view);
-        } catch (ClassNotFoundException ignored) {}
+        } catch (ClassNotFoundException ignored) {
+        }
 
         return isRecyclerView;
     }
@@ -640,6 +641,17 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         invalidateActions();
     }
 
+    private Drawable getButtonSelector() {
+        if (isStacked) {
+            Drawable custom = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_selector);
+            if (custom != null) return custom;
+        } else {
+            Drawable custom = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_selector);
+            if (custom != null) return custom;
+        }
+        return DialogUtils.resolveDrawable(getContext(), isStacked ? R.attr.md_selector : R.attr.md_btn_selector);
+    }
+
     /**
      * Invalidates the positive/neutral/negative action buttons. Decides whether they should be visible
      * and sets their properties (such as height, text color, etc.).
@@ -668,8 +680,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             setTypeface(positiveTextView, mBuilder.mediumFont);
             positiveTextView.setText(mBuilder.positiveText);
             positiveTextView.setTextColor(getActionTextStateList(mBuilder.positiveColor));
-            setBackgroundCompat(positiveButton, DialogUtils.resolveDrawable(getContext(),
-                    isStacked ? R.attr.md_selector : R.attr.md_btn_selector));
+            setBackgroundCompat(positiveButton, getButtonSelector());
             positiveButton.setTag(POSITIVE);
             positiveButton.setOnClickListener(this);
         } else {
@@ -683,7 +694,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             setTypeface(neutralTextView, mBuilder.mediumFont);
             neutralButton.setVisibility(View.VISIBLE);
             neutralTextView.setTextColor(getActionTextStateList(mBuilder.neutralColor));
-            setBackgroundCompat(neutralButton, DialogUtils.resolveDrawable(getContext(), isStacked ? R.attr.md_selector : R.attr.md_btn_selector));
+            setBackgroundCompat(neutralButton, getButtonSelector());
             neutralTextView.setText(mBuilder.neutralText);
             neutralButton.setTag(NEUTRAL);
             neutralButton.setOnClickListener(this);
@@ -698,16 +709,14 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             setTypeface(negativeTextView, mBuilder.mediumFont);
             negativeButton.setVisibility(View.VISIBLE);
             negativeTextView.setTextColor(getActionTextStateList(mBuilder.negativeColor));
-            setBackgroundCompat(negativeButton, DialogUtils.resolveDrawable(getContext(),
-                    isStacked ? R.attr.md_selector : R.attr.md_btn_selector));
+            setBackgroundCompat(negativeButton, getButtonSelector());
             negativeTextView.setText(mBuilder.negativeText);
             negativeButton.setTag(NEGATIVE);
             negativeButton.setOnClickListener(this);
 
             if (!isStacked) {
                 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
-                        RelativeLayout.LayoutParams.WRAP_CONTENT,
-                        (int) getContext().getResources().getDimension(R.dimen.md_button_height));
+                        RelativeLayout.LayoutParams.WRAP_CONTENT, (int) getContext().getResources().getDimension(R.dimen.md_button_height));
                 if (mBuilder.positiveText != null) {
                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                         params.addRule(RelativeLayout.START_OF, R.id.buttonDefaultPositive);
@@ -849,6 +858,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         protected int dividerColor;
         protected int backgroundColor;
         protected int itemColor;
+        protected Drawable selector;
+        protected Drawable btnSelector;
 
         public Builder(@NonNull Context context) {
             this.context = context;
@@ -901,6 +912,10 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
                 backgroundColor(s.backgroundColor);
             if (s.dividerColor != 0)
                 dividerColor(s.dividerColor);
+            if (s.selector != null)
+                selector(s.selector);
+            if (s.btnSelector != null)
+                btnSelector(s.btnSelector);
         }
 
         public Builder title(@StringRes int titleRes) {
@@ -1092,6 +1107,24 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             return this;
         }
 
+        public Builder selectorRes(@DrawableRes int selectorRes) {
+            return selector(this.context.getResources().getDrawable(selectorRes));
+        }
+
+        public Builder selector(Drawable selector) {
+            this.selector = selector;
+            return this;
+        }
+
+        public Builder btnSelectorRes(@DrawableRes int selectorRes) {
+            return btnSelector(this.context.getResources().getDrawable(selectorRes));
+        }
+
+        public Builder btnSelector(Drawable selector) {
+            this.btnSelector = selector;
+            return this;
+        }
+
         /**
          * Use {@link #customView(int, boolean)} instead.
          */
@@ -1274,7 +1307,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
 
     @Override
     public void show() {
-        if(Looper.myLooper() != Looper.getMainLooper())
+        if (Looper.myLooper() != Looper.getMainLooper())
             throw new IllegalStateException("Dialogs can only be shown from the UI thread.");
         super.show();
     }
@@ -1517,7 +1550,6 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         public View getView(final int index, View convertView, ViewGroup parent) {
             final View view = super.getView(index, convertView, parent);
             TextView tv = (TextView) view.findViewById(R.id.title);
-
             switch (listType) {
                 case SINGLE: {
                     @SuppressLint("CutPasteId")
@@ -1532,13 +1564,15 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
                     break;
                 }
             }
-
             tv.setText(mBuilder.items[index]);
             tv.setTextColor(itemColor);
             setTypeface(tv, mBuilder.regularFont);
-
             view.setTag(index + ":" + mBuilder.items[index]);
 
+            Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_selector);
+            if (d == null)
+                d = DialogUtils.resolveDrawable(getContext(), R.attr.md_selector);
+            setBackgroundCompat(view, d);
             return view;
         }
     }

+ 2 - 9
library/src/main/java/com/afollestad/materialdialogs/ThemeSingleton.java

@@ -19,15 +19,6 @@ public class ThemeSingleton {
         return get(true);
     }
 
-//    <attr name="md_dark_theme" format="boolean" />
-//    <attr name="md_title_color" format="color" />
-//    <attr name="md_content_color" format="color" />
-//    <attr name="md_accent_color" format="color" />
-//    <attr name="md_item_color" format="color" />
-//    <attr name="md_icon" format="reference" />
-//    <attr name="md_background_color" format="color" />
-//    <attr name="md_divider_color" format="color" />
-
     public boolean darkTheme = false;
     public int titleColor = 0;
     public int contentColor = 0;
@@ -36,4 +27,6 @@ public class ThemeSingleton {
     public Drawable icon = null;
     public int backgroundColor = 0;
     public int dividerColor = 0;
+    public Drawable selector;
+    public Drawable btnSelector;
 }

+ 2 - 2
library/src/main/res/values/attrs.xml

@@ -2,8 +2,6 @@
 <resources>
 
     <!-- These top three attributes are not accessible to library users -->
-    <attr name="md_selector" format="reference" />
-    <attr name="md_btn_selector" format="reference" />
     <attr name="md_divider" format="color" />
 
     <!-- Accessible global theming attributes -->
@@ -15,5 +13,7 @@
     <attr name="md_icon" format="reference" />
     <attr name="md_background_color" format="color" />
     <attr name="md_divider_color" format="color" />
+    <attr name="md_selector" format="reference" />
+    <attr name="md_btn_selector" format="reference" />
 
 </resources>