dialog preferences

This commit is contained in:
serso 2011-09-27 17:28:49 +04:00
parent fca272dc49
commit d86d8c657b
4 changed files with 90 additions and 59 deletions

View File

@ -25,9 +25,12 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
@NotNull @NotNull
protected final static String androidns = "http://schemas.android.com/apk/res/android"; protected final static String androidns = "http://schemas.android.com/apk/res/android";
@NotNull @Nullable
protected TextView valueTextView; protected TextView valueTextView;
@Nullable
protected String valueText;
@NotNull @NotNull
protected final Context context; protected final Context context;
@ -40,17 +43,16 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
@Nullable @Nullable
private T defaultValue; private T defaultValue;
@Nullable
protected String valueText;
@Nullable @Nullable
private final String defaultStringValue; 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); super(context, attrs);
this.context = context; this.context = context;
this.defaultStringValue = defaultStringValue; this.defaultStringValue = defaultStringValue;
this.needValueText = needValueText;
final String defaultValueFromAttrs = attrs.getAttributeValue(androidns, "defaultValue"); final String defaultValueFromAttrs = attrs.getAttributeValue(androidns, "defaultValue");
if ( defaultValueFromAttrs != null ) { if ( defaultValueFromAttrs != null ) {
@ -67,7 +69,7 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
@Override @Override
@NotNull @NotNull
protected LinearLayout onCreateDialogView() { protected final LinearLayout onCreateDialogView() {
if (shouldPersist()) { if (shouldPersist()) {
value = getPersistedValue(); value = getPersistedValue();
} }
@ -82,6 +84,7 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
result.addView(splashText); result.addView(splashText);
} }
if (needValueText) {
valueTextView = new TextView(context); valueTextView = new TextView(context);
valueTextView.setGravity(Gravity.CENTER_HORIZONTAL); valueTextView.setGravity(Gravity.CENTER_HORIZONTAL);
valueTextView.setTextSize(32); valueTextView.setTextSize(32);
@ -90,10 +93,24 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams.WRAP_CONTENT);
result.addView(valueTextView, params); 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; return result;
} }
@Nullable
protected abstract LinearLayout.LayoutParams getParams();
@Override @Override
protected void onSetInitialValue(boolean restore, Object defaultValue) { protected void onSetInitialValue(boolean restore, Object defaultValue) {
super.onSetInitialValue(restore, defaultValue); super.onSetInitialValue(restore, defaultValue);
@ -115,10 +132,13 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
@Override @Override
protected void onBindDialogView(View v) { protected void onBindDialogView(View v) {
super.onBindDialogView(v); super.onBindDialogView(v);
initPreferenceView(); initPreferenceView(null);
} }
protected abstract void initPreferenceView(); @NotNull
protected abstract View createPreferenceView();
protected abstract void initPreferenceView(@Nullable View v);
@Nullable @Nullable
private T getPersistedValue() { private T getPersistedValue() {

View File

@ -9,6 +9,7 @@ package org.solovyev.android.view.prefs;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.Gravity; import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -31,7 +32,7 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference<Integ
private final Interval<Integer> boundaries; private final Interval<Integer> boundaries;
public NumberPickerDialogPreference(Context context, AttributeSet attrs) { public NumberPickerDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs, null); super(context, attrs, null, false);
//noinspection ConstantConditions //noinspection ConstantConditions
boundaries = new GenericIntervalMapper<Integer>(getMapper()).parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); boundaries = new GenericIntervalMapper<Integer>(getMapper()).parseValue(attrs.getAttributeValue(localNameSpace, "boundaries"));
@ -39,33 +40,35 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference<Integ
createPreferenceView(); createPreferenceView();
} }
@NotNull
@Override @Override
protected LinearLayout onCreateDialogView() { protected LinearLayout.LayoutParams getParams() {
final LinearLayout result = super.onCreateDialogView(); final LinearLayout.LayoutParams result = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
result.gravity = Gravity.CENTER;
createPreferenceView();
initPreferenceView();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
result.addView(numberPicker, params);
return result; return result;
} }
protected void createPreferenceView() { @NotNull
@Override
protected View createPreferenceView() {
this.numberPicker = new NumberPicker(context); this.numberPicker = new NumberPicker(context);
this.numberPicker.setOnChangeListener(this); this.numberPicker.setOnChangeListener(this);
initPreferenceView(); initPreferenceView(this.numberPicker);
return numberPicker;
} }
@Override @Override
protected void initPreferenceView() { protected void initPreferenceView(@Nullable View v) {
if ( v == null ) {
v = numberPicker;
}
if (value != null) { if (value != null) {
numberPicker.setRange(boundaries.getLeftBorder(), boundaries.getRightBorder()); ((NumberPicker) v).setRange(boundaries.getLeftBorder(), boundaries.getRightBorder());
numberPicker.setCurrent(value); ((NumberPicker) v).setCurrent(value);
} }
} }

View File

@ -2,8 +2,10 @@ package org.solovyev.android.view.prefs;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.view.widgets.AbstractRangeSeekBar; import org.solovyev.android.view.widgets.AbstractRangeSeekBar;
import org.solovyev.android.view.widgets.NumberRangeSeekBar; import org.solovyev.android.view.widgets.NumberRangeSeekBar;
import org.solovyev.common.utils.Interval; import org.solovyev.common.utils.Interval;
@ -25,7 +27,7 @@ public abstract class RangeSeekBarPreference<T extends Number> extends AbstractD
private Integer steps; private Integer steps;
public RangeSeekBarPreference(@NotNull Context context, AttributeSet attrs) { public RangeSeekBarPreference(@NotNull Context context, AttributeSet attrs) {
super(context, attrs, null); super(context, attrs, null, true);
//noinspection ConstantConditions //noinspection ConstantConditions
boundaries = getMapper().parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); boundaries = getMapper().parseValue(attrs.getAttributeValue(localNameSpace, "boundaries"));
@ -40,31 +42,31 @@ public abstract class RangeSeekBarPreference<T extends Number> extends AbstractD
createPreferenceView(); createPreferenceView();
} }
private void createPreferenceView() { @NotNull
protected View createPreferenceView() {
this.rangeSeekBar = new NumberRangeSeekBar<T>(boundaries, steps, context); this.rangeSeekBar = new NumberRangeSeekBar<T>(boundaries, steps, context);
this.rangeSeekBar.setNotifyWhileDragging(true); this.rangeSeekBar.setNotifyWhileDragging(true);
this.rangeSeekBar.setOnRangeSeekBarChangeListener(this); this.rangeSeekBar.setOnRangeSeekBarChangeListener(this);
initPreferenceView(); initPreferenceView(this.rangeSeekBar);
}
@NotNull return this.rangeSeekBar;
@Override
protected LinearLayout onCreateDialogView() {
final LinearLayout result = super.onCreateDialogView();
createPreferenceView();
result.addView(rangeSeekBar);
return result;
} }
@Override @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) { if (value != null) {
rangeSeekBar.setSelectedMinValue(value.getLeftBorder()); ((AbstractRangeSeekBar<T>) v).setSelectedMinValue(value.getLeftBorder());
rangeSeekBar.setSelectedMaxValue(value.getRightBorder()); ((AbstractRangeSeekBar<T>) v).setSelectedMaxValue(value.getRightBorder());
setValueText(value); setValueText(value);
} }
} }

View File

@ -8,9 +8,11 @@ package org.solovyev.android.view.prefs;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.SeekBar; import android.widget.SeekBar;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.NumberMapper; import org.solovyev.common.NumberMapper;
import org.solovyev.common.utils.Mapper; import org.solovyev.common.utils.Mapper;
@ -29,30 +31,34 @@ public class SeekBarPreference extends AbstractDialogPreference<Integer> impleme
private int max = 0; private int max = 0;
public SeekBarPreference(Context context, AttributeSet attrs) { public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs, "50"); super(context, attrs, "50", true);
max = attrs.getAttributeIntValue(androidns, "max", 100); max = attrs.getAttributeIntValue(androidns, "max", 100);
} }
@Override
protected LinearLayout.LayoutParams getParams() {
return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
@NotNull @NotNull
@Override @Override
protected LinearLayout onCreateDialogView() { protected View createPreferenceView() {
final LinearLayout layout = super.onCreateDialogView();
seekBar = new SeekBar(context); seekBar = new SeekBar(context);
seekBar.setOnSeekBarChangeListener(this); seekBar.setOnSeekBarChangeListener(this);
layout.addView(seekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
initPreferenceView(); return seekBar;
return layout;
} }
@Override @Override
protected void initPreferenceView() { protected void initPreferenceView(@Nullable View v ) {
seekBar.setMax(max); if ( v == null) {
v = seekBar;
}
((SeekBar) v).setMax(max);
if (value != null) { if (value != null) {
seekBar.setProgress(value); ((SeekBar) v).setProgress(value);
setValueText(value); setValueText(value);
} }
} }