From 5b741c827ab9fd99b5de1eb496143edc4f8fe3ca Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 27 Sep 2011 17:28:49 +0400 Subject: [PATCH] dialog preferences --- .../view/prefs/AbstractDialogPreference.java | 50 +++++++++++++------ .../prefs/NumberPickerDialogPreference.java | 35 +++++++------ .../view/prefs/RangeSeekBarPreference.java | 36 ++++++------- .../android/view/prefs/SeekBarPreference.java | 28 +++++++---- 4 files changed, 90 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java b/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java index 78f9a187..f7ebc3cf 100644 --- a/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java @@ -25,9 +25,12 @@ public abstract class AbstractDialogPreference extends DialogPreference { @NotNull protected final static String androidns = "http://schemas.android.com/apk/res/android"; - @NotNull + @Nullable protected TextView valueTextView; + @Nullable + protected String valueText; + @NotNull protected final Context context; @@ -40,17 +43,16 @@ public abstract class AbstractDialogPreference extends DialogPreference { @Nullable private T defaultValue; - @Nullable - protected String valueText; - @Nullable private final String defaultStringValue; + private final boolean needValueText; - public AbstractDialogPreference(Context context, AttributeSet attrs, @Nullable String defaultStringValue) { + public AbstractDialogPreference(Context context, AttributeSet attrs, @Nullable String defaultStringValue, boolean needValueText) { super(context, attrs); this.context = context; this.defaultStringValue = defaultStringValue; + this.needValueText = needValueText; final String defaultValueFromAttrs = attrs.getAttributeValue(androidns, "defaultValue"); if ( defaultValueFromAttrs != null ) { @@ -67,7 +69,7 @@ public abstract class AbstractDialogPreference extends DialogPreference { @Override @NotNull - protected LinearLayout onCreateDialogView() { + protected final LinearLayout onCreateDialogView() { if (shouldPersist()) { value = getPersistedValue(); } @@ -82,18 +84,33 @@ public abstract class AbstractDialogPreference extends DialogPreference { result.addView(splashText); } - valueTextView = new TextView(context); - valueTextView.setGravity(Gravity.CENTER_HORIZONTAL); - valueTextView.setTextSize(32); + if (needValueText) { + valueTextView = new TextView(context); + valueTextView.setGravity(Gravity.CENTER_HORIZONTAL); + valueTextView.setTextSize(32); - final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.FILL_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT); - result.addView(valueTextView, params); + final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.FILL_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT); + result.addView(valueTextView, params); + } + + final View v = createPreferenceView(); + initPreferenceView(v); + + final LinearLayout.LayoutParams params = getParams(); + if (params != null) { + result.addView(v); + } else { + result.addView(v, params); + } return result; } + @Nullable + protected abstract LinearLayout.LayoutParams getParams(); + @Override protected void onSetInitialValue(boolean restore, Object defaultValue) { super.onSetInitialValue(restore, defaultValue); @@ -115,10 +132,13 @@ public abstract class AbstractDialogPreference extends DialogPreference { @Override protected void onBindDialogView(View v) { super.onBindDialogView(v); - initPreferenceView(); + initPreferenceView(null); } - protected abstract void initPreferenceView(); + @NotNull + protected abstract View createPreferenceView(); + + protected abstract void initPreferenceView(@Nullable View v); @Nullable private T getPersistedValue() { diff --git a/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java b/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java index 861817a3..cf848f03 100644 --- a/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java @@ -9,6 +9,7 @@ package org.solovyev.android.view.prefs; import android.content.Context; import android.util.AttributeSet; import android.view.Gravity; +import android.view.View; import android.widget.LinearLayout; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -31,7 +32,7 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference boundaries; public NumberPickerDialogPreference(Context context, AttributeSet attrs) { - super(context, attrs, null); + super(context, attrs, null, false); //noinspection ConstantConditions boundaries = new GenericIntervalMapper(getMapper()).parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); @@ -39,33 +40,35 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference extends AbstractD private Integer steps; public RangeSeekBarPreference(@NotNull Context context, AttributeSet attrs) { - super(context, attrs, null); + super(context, attrs, null, true); //noinspection ConstantConditions boundaries = getMapper().parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); @@ -40,31 +42,31 @@ public abstract class RangeSeekBarPreference extends AbstractD createPreferenceView(); } - private void createPreferenceView() { + @NotNull + protected View createPreferenceView() { this.rangeSeekBar = new NumberRangeSeekBar(boundaries, steps, context); this.rangeSeekBar.setNotifyWhileDragging(true); this.rangeSeekBar.setOnRangeSeekBarChangeListener(this); - initPreferenceView(); - } + initPreferenceView(this.rangeSeekBar); - @NotNull - @Override - protected LinearLayout onCreateDialogView() { - final LinearLayout result = super.onCreateDialogView(); - - createPreferenceView(); - - result.addView(rangeSeekBar); - - return result; + return this.rangeSeekBar; } @Override - protected void initPreferenceView() { + protected LinearLayout.LayoutParams getParams() { + return null; + } + + @Override + protected void initPreferenceView(@Nullable View v) { + if ( v == null ) { + v = rangeSeekBar; + } + if (value != null) { - rangeSeekBar.setSelectedMinValue(value.getLeftBorder()); - rangeSeekBar.setSelectedMaxValue(value.getRightBorder()); + ((AbstractRangeSeekBar) v).setSelectedMinValue(value.getLeftBorder()); + ((AbstractRangeSeekBar) v).setSelectedMaxValue(value.getRightBorder()); setValueText(value); } } diff --git a/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java index bd3621ec..869ea0e9 100644 --- a/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java @@ -8,9 +8,11 @@ package org.solovyev.android.view.prefs; import android.content.Context; import android.util.AttributeSet; +import android.view.View; import android.widget.LinearLayout; import android.widget.SeekBar; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.solovyev.common.NumberMapper; import org.solovyev.common.utils.Mapper; @@ -29,30 +31,34 @@ public class SeekBarPreference extends AbstractDialogPreference impleme private int max = 0; public SeekBarPreference(Context context, AttributeSet attrs) { - super(context, attrs, "50"); + super(context, attrs, "50", true); max = attrs.getAttributeIntValue(androidns, "max", 100); } + @Override + protected LinearLayout.LayoutParams getParams() { + return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); + } + @NotNull @Override - protected LinearLayout onCreateDialogView() { - final LinearLayout layout = super.onCreateDialogView(); - + protected View createPreferenceView() { seekBar = new SeekBar(context); seekBar.setOnSeekBarChangeListener(this); - layout.addView(seekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); - initPreferenceView(); - - return layout; + return seekBar; } @Override - protected void initPreferenceView() { - seekBar.setMax(max); + protected void initPreferenceView(@Nullable View v ) { + if ( v == null) { + v = seekBar; + } + + ((SeekBar) v).setMax(max); if (value != null) { - seekBar.setProgress(value); + ((SeekBar) v).setProgress(value); setValueText(value); } }