Browse Source

Color chooser dialog works, not done yet though. RecyclerViews do not correctly measure height (in the case of wrap_content) with the current stock layout managers, so I switched away from that.

Aidan Follestad 10 years ago
parent
commit
f9670d28fc

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

@@ -581,7 +581,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             GridLayoutManager glm = (GridLayoutManager) lm;
             lastVisible = glm.findLastVisibleItemPosition();
         } else {
-            throw new NotImplementedException("MaterialDialogs currently only supports LinearLayoutManager and GridLayoutManager. Please report any new layout managers.");
+            throw new NotImplementedException("Material Dialogs currently only supports LinearLayoutManager and GridLayoutManager. Please report any new layout managers.");
         }
 
         if (lastVisible == -1)

+ 1 - 1
sample/build.gradle

@@ -6,7 +6,7 @@ android {
 
     defaultConfig {
         applicationId "com.afollestad.materialdialogssample"
-        minSdkVersion 11
+        minSdkVersion 14
         targetSdkVersion 21
         versionCode 59
         versionName "0.5.3"

+ 0 - 79
sample/src/main/java/com/afollestad/materialdialogssample/ColorChooserAdapter.java

@@ -1,79 +0,0 @@
-package com.afollestad.materialdialogssample;
-
-import android.content.Context;
-import android.content.res.TypedArray;
-import android.graphics.drawable.ShapeDrawable;
-import android.graphics.drawable.shapes.OvalShape;
-import android.os.Build;
-import android.support.v7.widget.RecyclerView;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-
-/**
- * @author Aidan Follestad (afollestad)
- */
-public class ColorChooserAdapter extends RecyclerView.Adapter<ColorChooserAdapter.ViewHolder> implements View.OnClickListener {
-
-    private Callback mCallback;
-    private int[] mColors;
-
-    @Override
-    public void onClick(View v) {
-        if (mCallback != null) {
-            Integer index = (Integer) v.getTag();
-            mCallback.onColorSelected(index, mColors[index]);
-        }
-    }
-
-    public static interface Callback {
-        void onColorSelected(int index, int primary);
-    }
-
-    public static class ViewHolder extends RecyclerView.ViewHolder {
-
-        public View mView;
-        public View mColorView;
-
-        public ViewHolder(View v) {
-            super(v);
-            mView = v;
-            mColorView = v.findViewById(R.id.color);
-        }
-    }
-
-    public ColorChooserAdapter(Context context, Callback callback) {
-        mCallback = callback;
-        TypedArray ta = context.getResources().obtainTypedArray(R.array.material_colors_500);
-        mColors = new int[ta.length()];
-        for (int i = 0; i < ta.length(); i++)
-            mColors[i] = ta.getColor(i, 0);
-        ta.recycle();
-    }
-
-    @Override
-    public ColorChooserAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
-        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_colorchooser, null);
-        return new ViewHolder(v);
-    }
-
-    @Override
-    public void onBindViewHolder(ViewHolder holder, int position) {
-        holder.mView.setTag(position);
-        holder.mView.setOnClickListener(this);
-
-        ShapeDrawable circle = new ShapeDrawable(new OvalShape());
-        circle.getPaint().setColor(mColors[position]);
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
-            holder.mColorView.setBackground(circle);
-        else holder.mColorView.setBackgroundDrawable(circle);
-
-        // TODO add circular ripple selector over the colored circles here
-        // TODO add activated state to circles, check mark similar to Today Calendar's color chooser
-    }
-
-    @Override
-    public int getItemCount() {
-        return mColors.length;
-    }
-}

+ 21 - 13
sample/src/main/java/com/afollestad/materialdialogssample/ColorChooserDialog.java

@@ -3,24 +3,23 @@ package com.afollestad.materialdialogssample;
 import android.app.Activity;
 import android.app.Dialog;
 import android.app.DialogFragment;
+import android.content.res.TypedArray;
+import android.graphics.drawable.ShapeDrawable;
+import android.graphics.drawable.shapes.OvalShape;
+import android.os.Build;
 import android.os.Bundle;
-import android.support.v7.widget.GridLayoutManager;
-import android.support.v7.widget.RecyclerView;
+import android.view.View;
+import android.widget.GridLayout;
 
 import com.afollestad.materialdialogs.MaterialDialog;
 
 /**
  * @author Aidan Follestad (afollestad)
  */
-public class ColorChooserDialog extends DialogFragment implements ColorChooserAdapter.Callback {
+public class ColorChooserDialog extends DialogFragment {
 
     private Callback mCallback;
 
-    @Override
-    public void onColorSelected(int index, int primary) {
-        mCallback.onColorSelection(primary);
-    }
-
     public static interface Callback {
         void onColorSelection(int primary);
     }
@@ -34,12 +33,21 @@ public class ColorChooserDialog extends DialogFragment implements ColorChooserAd
                 .title(R.string.color_chooser)
                 .autoDismiss(false)
                 .customView(R.layout.dialog_color_chooser, false)
-                .positiveText(R.string.choose)
                 .build();
-        RecyclerView list = (RecyclerView) dialog.getCustomView().findViewById(R.id.list);
-        if (list != null) {
-            list.setLayoutManager(new GridLayoutManager(getActivity(), getResources().getInteger(R.integer.color_chooser_columns)));
-            list.setAdapter(new ColorChooserAdapter(getActivity(), this));
+
+        TypedArray ta = getActivity().getResources().obtainTypedArray(R.array.material_colors_500);
+        int[] mColors = new int[ta.length()];
+        for (int i = 0; i < ta.length(); i++)
+            mColors[i] = ta.getColor(i, 0);
+        ta.recycle();
+        GridLayout list = (GridLayout) dialog.getCustomView().findViewById(R.id.grid);
+        for (int i = 0; i < list.getChildCount(); i++) {
+            View child = list.getChildAt(i);
+            ShapeDrawable circle = new ShapeDrawable(new OvalShape());
+            circle.getPaint().setColor(mColors[i]);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
+                child.setBackground(circle);
+            else child.setBackgroundDrawable(circle);
         }
         return dialog;
     }

+ 114 - 9
sample/src/main/res/layout/dialog_color_chooser.xml

@@ -1,17 +1,122 @@
-<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
+    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
+    android:orientation="vertical"
     android:gravity="center_horizontal">
 
-    <android.support.v7.widget.RecyclerView
-        android:id="@+id/list"
-        android:scrollbars="vertical"
-        android:layout_width="256dp"
+    <GridLayout
+        android:id="@+id/grid"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:paddingTop="8dp"
-        android:paddingBottom="8dp"
-        android:clipToPadding="false" />
+        android:columnCount="4"
+        android:rowCount="5"
+        android:orientation="vertical"
+        tools:ignore="UselessParent"
+        android:paddingTop="4dp"
+        android:paddingBottom="16dp"
+        android:clipToPadding="false">
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+        <View
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:layout_margin="4dp" />
+
+    </GridLayout>
 
 </LinearLayout>

+ 6 - 14
sample/src/main/res/layout/list_item_colorchooser.xml

@@ -1,15 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:padding="4dp">
-
-    <View
-        android:id="@+id/color"
-        android:layout_width="56dp"
-        android:layout_height="56dp"
-        android:background="#000"
-        android:layout_gravity="center" />
-
-</LinearLayout>
+<View xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/color"
+    android:layout_width="56dp"
+    android:layout_height="56dp"
+    android:background="#000"
+    android:layout_gravity="center" />