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
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<T> 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<T> extends DialogPreference {
@Override
@NotNull
protected LinearLayout onCreateDialogView() {
protected final LinearLayout onCreateDialogView() {
if (shouldPersist()) {
value = getPersistedValue();
}
@ -82,6 +84,7 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
result.addView(splashText);
}
if (needValueText) {
valueTextView = new TextView(context);
valueTextView.setGravity(Gravity.CENTER_HORIZONTAL);
valueTextView.setTextSize(32);
@ -90,10 +93,24 @@ public abstract class AbstractDialogPreference<T> extends DialogPreference {
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<T> 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() {

View File

@ -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<Integ
private final Interval<Integer> boundaries;
public NumberPickerDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs, null);
super(context, attrs, null, false);
//noinspection ConstantConditions
boundaries = new GenericIntervalMapper<Integer>(getMapper()).parseValue(attrs.getAttributeValue(localNameSpace, "boundaries"));
@ -39,33 +40,35 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference<Integ
createPreferenceView();
}
@NotNull
@Override
protected LinearLayout onCreateDialogView() {
final LinearLayout result = super.onCreateDialogView();
createPreferenceView();
initPreferenceView();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
result.addView(numberPicker, params);
protected LinearLayout.LayoutParams getParams() {
final LinearLayout.LayoutParams result = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
result.gravity = Gravity.CENTER;
return result;
}
protected void createPreferenceView() {
@NotNull
@Override
protected View createPreferenceView() {
this.numberPicker = new NumberPicker(context);
this.numberPicker.setOnChangeListener(this);
initPreferenceView();
initPreferenceView(this.numberPicker);
return numberPicker;
}
@Override
protected void initPreferenceView() {
protected void initPreferenceView(@Nullable View v) {
if ( v == null ) {
v = numberPicker;
}
if (value != null) {
numberPicker.setRange(boundaries.getLeftBorder(), boundaries.getRightBorder());
numberPicker.setCurrent(value);
((NumberPicker) v).setRange(boundaries.getLeftBorder(), boundaries.getRightBorder());
((NumberPicker) v).setCurrent(value);
}
}

View File

@ -2,8 +2,10 @@ package org.solovyev.android.view.prefs;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.view.widgets.AbstractRangeSeekBar;
import org.solovyev.android.view.widgets.NumberRangeSeekBar;
import org.solovyev.common.utils.Interval;
@ -25,7 +27,7 @@ public abstract class RangeSeekBarPreference<T extends Number> 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<T extends Number> extends AbstractD
createPreferenceView();
}
private void createPreferenceView() {
@NotNull
protected View createPreferenceView() {
this.rangeSeekBar = new NumberRangeSeekBar<T>(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<T>) v).setSelectedMinValue(value.getLeftBorder());
((AbstractRangeSeekBar<T>) v).setSelectedMaxValue(value.getRightBorder());
setValueText(value);
}
}

View File

@ -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<Integer> 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);
}
}