Procházet zdrojové kódy

Custom color frame can scroll, e.g. when in landscape on a phone. Color chooser maintains state through device rotation.

Aidan Follestad před 9 roky
rodič
revize
69f47f698e

+ 39 - 23
commons/src/main/java/com/afollestad/materialdialogs/color/ColorChooserDialog.java

@@ -100,6 +100,15 @@ public class ColorChooserDialog extends DialogFragment implements View.OnClickLi
     private SeekBar.OnSeekBarChangeListener mCustomColorRgbListener;
     private int mSelectedCustomColor;
 
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putInt("top_index", topIndex());
+        outState.putBoolean("in_sub", isInSub());
+        outState.putInt("sub_index", subIndex());
+        outState.putBoolean("in_custom", mColorChooserCustomFrame.getVisibility() == View.VISIBLE);
+    }
+
     @Override
     public void onAttach(Activity activity) {
         super.onAttach(activity);
@@ -258,33 +267,40 @@ public class ColorChooserDialog extends DialogFragment implements View.OnClickLi
             throw new IllegalStateException("ColorChooserDialog should be created using its Builder interface.");
         generateColors();
 
-        final int preselectColor = getBuilder().mPreselect;
+        int preselectColor;
         boolean foundPreselectColor = false;
-        if (preselectColor != 0) {
-            for (int topIndex = 0; topIndex < mColorsTop.length; topIndex++) {
-                if (mColorsTop[topIndex] == preselectColor) {
-                    foundPreselectColor = true;
-                    topIndex(topIndex);
-                    if (getBuilder().mAccentMode) {
-                        subIndex(2);
-                    } else if (mColorsSub != null) {
-                        findSubIndexForColor(topIndex, preselectColor);
-                    } else {
-                        subIndex(5);
+
+        if (savedInstanceState != null) {
+            foundPreselectColor = !savedInstanceState.getBoolean("in_custom", false);
+            preselectColor = getSelectedColor();
+        } else {
+            preselectColor = getBuilder().mPreselect;
+            if (preselectColor != 0) {
+                for (int topIndex = 0; topIndex < mColorsTop.length; topIndex++) {
+                    if (mColorsTop[topIndex] == preselectColor) {
+                        foundPreselectColor = true;
+                        topIndex(topIndex);
+                        if (getBuilder().mAccentMode) {
+                            subIndex(2);
+                        } else if (mColorsSub != null) {
+                            findSubIndexForColor(topIndex, preselectColor);
+                        } else {
+                            subIndex(5);
+                        }
+                        break;
                     }
-                    break;
-                }
 
-                if (mColorsSub != null) {
-                    for (int subIndex = 0; subIndex < mColorsSub[topIndex].length; subIndex++) {
-                        if (mColorsSub[topIndex][subIndex] == preselectColor) {
-                            foundPreselectColor = true;
-                            topIndex(topIndex);
-                            subIndex(subIndex);
-                            break;
+                    if (mColorsSub != null) {
+                        for (int subIndex = 0; subIndex < mColorsSub[topIndex].length; subIndex++) {
+                            if (mColorsSub[topIndex][subIndex] == preselectColor) {
+                                foundPreselectColor = true;
+                                topIndex(topIndex);
+                                subIndex(subIndex);
+                                break;
+                            }
                         }
+                        if (foundPreselectColor) break;
                     }
-                    if (foundPreselectColor) break;
                 }
             }
         }
@@ -377,7 +393,7 @@ public class ColorChooserDialog extends DialogFragment implements View.OnClickLi
             dialog.setTitle(getBuilder().mCustomBtn);
             dialog.setActionButton(DialogAction.NEUTRAL, getBuilder().mPresetsBtn);
             dialog.setActionButton(DialogAction.NEGATIVE, getBuilder().mCancelBtn);
-            mGrid.setVisibility(View.GONE);
+            mGrid.setVisibility(View.INVISIBLE);
             mColorChooserCustomFrame.setVisibility(View.VISIBLE);
             mCustomColorTextWatcher = new TextWatcher() {
                 @Override

+ 30 - 0
commons/src/main/java/com/afollestad/materialdialogs/color/FillGridView.java

@@ -0,0 +1,30 @@
+package com.afollestad.materialdialogs.color;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.widget.GridView;
+
+/**
+ * @author Aidan Follestad (afollestad)
+ */
+public class FillGridView extends GridView {
+
+    public FillGridView(Context context) {
+        super(context);
+    }
+
+    public FillGridView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public FillGridView(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    @Override
+    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
+                MeasureSpec.AT_MOST);
+        super.onMeasure(widthMeasureSpec, expandSpec);
+    }
+}

+ 14 - 8
commons/src/main/res/layout/md_dialog_colorchooser.xml

@@ -1,13 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
+    android:layout_height="match_parent">
 
-    <include layout="@layout/md_stub_colorchooser_grid" />
+    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
 
-    <include
-        layout="@layout/md_stub_colorchooser_custom"
-        android:visibility="gone" />
+        <include layout="@layout/md_stub_colorchooser_grid" />
 
-</FrameLayout>
+        <include
+            layout="@layout/md_stub_colorchooser_custom"
+            android:visibility="gone" />
+
+    </FrameLayout>
+
+</ScrollView>

+ 1 - 1
commons/src/main/res/layout/md_stub_colorchooser_custom.xml

@@ -3,7 +3,7 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/colorChooserCustomFrame"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
+    android:layout_height="wrap_content"
     android:orientation="vertical"
     android:paddingBottom="@dimen/md_title_frame_margin_bottom"
     android:paddingTop="@dimen/md_title_frame_margin_bottom">

+ 1 - 1
commons/src/main/res/layout/md_stub_colorchooser_grid.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<GridView xmlns:android="http://schemas.android.com/apk/res/android"
+<com.afollestad.materialdialogs.color.FillGridView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/grid"
     android:layout_width="match_parent"
     android:layout_height="match_parent"