Ver código fonte

Fixed coloring of check boxes and radio boxes when a dialog theme is specified that doesn't match the Activity theme (light/dark).

Aidan Follestad 10 anos atrás
pai
commit
c5e79ec6d2

+ 19 - 19
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -81,28 +81,28 @@ class DialogInit {
 
         // Retrieve default title/content colors
         if (!builder.titleColorSet) {
-            final int titleColorFallback = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimary);
+            final int titleColorFallback = DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorPrimary);
             builder.titleColor = DialogUtils.resolveColor(builder.context, R.attr.md_title_color, titleColorFallback);
-            if (builder.titleColor == titleColorFallback) {
-                // Only check for light/dark if color wasn't set to md_title_color
-                if (DialogUtils.isColorDark(builder.titleColor)) {
-                    if (builder.theme == Theme.DARK)
-                        builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimaryInverse);
-                } else if (builder.theme == Theme.LIGHT)
-                    builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimaryInverse);
-            }
+//            if (builder.titleColor == titleColorFallback) {
+//                // Only check for light/dark if color wasn't set to md_title_color
+//                if (DialogUtils.isColorDark(builder.titleColor)) {
+//                    if (builder.theme == Theme.DARK)
+//                        builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimaryInverse);
+//                } else if (builder.theme == Theme.LIGHT)
+//                    builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimaryInverse);
+//            }
         }
         if (!builder.contentColorSet) {
-            final int contentColorFallback = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondary);
+            final int contentColorFallback = DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorSecondary);
             builder.contentColor = DialogUtils.resolveColor(builder.context, R.attr.md_content_color, contentColorFallback);
-            if (builder.contentColor == contentColorFallback) {
-                // Only check for light/dark if color wasn't set to md_content_color
-                if (DialogUtils.isColorDark(builder.contentColor)) {
-                    if (builder.theme == Theme.DARK)
-                        builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondaryInverse);
-                } else if (builder.theme == Theme.LIGHT)
-                    builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondaryInverse);
-            }
+//            if (builder.contentColor == contentColorFallback) {
+//                // Only check for light/dark if color wasn't set to md_content_color
+//                if (DialogUtils.isColorDark(builder.contentColor)) {
+//                    if (builder.theme == Theme.DARK)
+//                        builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondaryInverse);
+//                } else if (builder.theme == Theme.LIGHT)
+//                    builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondaryInverse);
+//            }
         }
         if (!builder.itemColorSet)
             builder.itemColor = DialogUtils.resolveColor(builder.context, R.attr.md_item_color, builder.contentColor);
@@ -261,7 +261,7 @@ class DialogInit {
                     dialog.listType = MaterialDialog.ListType.REGULAR;
                 }
                 builder.adapter = new MaterialDialogAdapter(dialog,
-                        MaterialDialog.ListType.getLayoutForType(dialog.listType), R.id.title, builder.items);
+                        MaterialDialog.ListType.getLayoutForType(dialog.listType));
             } else if (builder.adapter instanceof MaterialSimpleListAdapter) {
                 // Notify simple list adapter of the dialog it belongs to
                 ((MaterialSimpleListAdapter) builder.adapter).setDialog(dialog, false);

+ 2 - 3
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -1309,13 +1309,12 @@ public class MaterialDialog extends DialogBase implements
     public final void setItems(CharSequence[] items) {
         if (mBuilder.adapter == null)
             throw new IllegalStateException("This MaterialDialog instance does not yet have an adapter set to it. You cannot use setItems().");
+        mBuilder.items = items;
         if (mBuilder.adapter instanceof MaterialDialogAdapter) {
-            mBuilder.adapter = new MaterialDialogAdapter(this,
-                    ListType.getLayoutForType(listType), R.id.title, items);
+            mBuilder.adapter = new MaterialDialogAdapter(this, ListType.getLayoutForType(listType));
         } else {
             throw new IllegalStateException("When using a custom adapter, setItems() cannot be used. Set items through the adapter instead.");
         }
-        mBuilder.items = items;
         listView.setAdapter(mBuilder.adapter);
     }
 

+ 25 - 7
library/src/main/java/com/afollestad/materialdialogs/MaterialDialogAdapter.java

@@ -4,10 +4,12 @@ import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
 import android.content.res.Configuration;
 import android.os.Build;
+import android.support.annotation.LayoutRes;
 import android.view.Gravity;
+import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ArrayAdapter;
+import android.widget.BaseAdapter;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.LinearLayout;
@@ -16,16 +18,19 @@ import android.widget.TextView;
 
 import com.afollestad.materialdialogs.internal.MDTintHelper;
 
-class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
+class MaterialDialogAdapter extends BaseAdapter {
 
     private final MaterialDialog dialog;
+    @LayoutRes
+    private final int layout;
+
     private final GravityEnum itemGravity;
     public RadioButton mRadioButton;
     public boolean mInitRadio;
 
-    public MaterialDialogAdapter(MaterialDialog dialog, int resource, int textViewResourceId, CharSequence[] objects) {
-        super(dialog.mBuilder.context, resource, textViewResourceId, objects);
+    public MaterialDialogAdapter(MaterialDialog dialog, @LayoutRes int layout) {
         this.dialog = dialog;
+        this.layout = layout;
         this.itemGravity = dialog.mBuilder.itemsGravity;
     }
 
@@ -34,6 +39,16 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
         return true;
     }
 
+    @Override
+    public int getCount() {
+        return dialog.mBuilder.items != null ? dialog.mBuilder.items.length : 0;
+    }
+
+    @Override
+    public Object getItem(int position) {
+        return dialog.mBuilder.items[position];
+    }
+
     @Override
     public long getItemId(int position) {
         return position;
@@ -41,8 +56,10 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
 
     @SuppressLint("WrongViewCast")
     @Override
-    public View getView(final int index, View convertView, ViewGroup parent) {
-        final View view = super.getView(index, convertView, parent);
+    public View getView(final int index, View view, ViewGroup parent) {
+        if (view == null)
+            view = LayoutInflater.from(dialog.getContext()).inflate(layout, parent, false);
+
         TextView tv = (TextView) view.findViewById(R.id.title);
         switch (dialog.listType) {
             case SINGLE: {
@@ -67,6 +84,7 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
         tv.setText(dialog.mBuilder.items[index]);
         tv.setTextColor(dialog.mBuilder.itemColor);
         dialog.setTypeface(tv, dialog.mBuilder.regularFont);
+
         view.setTag(index + ":" + dialog.mBuilder.items[index]);
         setupGravity((ViewGroup) view);
 
@@ -121,7 +139,7 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
     private boolean isRTL() {
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
             return false;
-        Configuration config = getContext().getResources().getConfiguration();
+        Configuration config = dialog.getBuilder().getContext().getResources().getConfiguration();
         return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
     }
 }