每次我想要一个SeekBar和相应的TextView显示它的数值时,为了尝试少写代码,我编写了以下抽象类:
abstract public class SeekBarWrapper {
SeekBar bar;
TextView valueText;
int value = 0;
int minValue;
int divisor;
public SeekBarWrapper(SeekBar sb, TextView tv, int value, int minValue,
int divisor){
this.bar = sb;
this.valueText = tv;
this.value = value;
this.minValue = minValue;
this.divisor = divisor;
setListener();
}
private void setListener(){
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(!fromUser) return;
value = progress + minValue;
valueText.setText(Integer.toString(value/divisor));
sendValue();
}
});
}
abstract protected void sendValue();
public void updateValue(int newValue){
if(newValue == value) return;
value = newValue;
valueText.setText(Integer.toString(value));
bar.setProgress(value*divisor-minValue);
}
}
对于每个具体的SeekBar,我将编写一个嵌套类,例如:
class VolumeBarWrapper extends SeekBarWrapper{
public VolumeBarWrapper(SeekBar s, TextView t, int v, int min, int div){
super(s, t, v, min, div);
}
public void sendValue(){
someCallback.volume(this.value);
}
}
并像这样实例化:
VolumeBarWrapper volume;
// later:
volume = new VolumeBarWrapper((SeekBar) view.findViewById(R.id.volume_bar),
(TextView) view.findViewById(R.id.volume_value), 300, 0, 70);
这是功能性的,似乎是一种改进。我想知道的是:
答案 0 :(得分:1)
您始终可以创建更好 SeekBar
,并在任何地方使用它:
public class VersatileSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
private TextView mTextView;
private ChangeHandler mChangeHandler;
public void bindDisplayToChange(TextView textView,ChangeHandler handler) {
mTextView = textView;
mChangeHandler = handler;
}
public VersatileSeekBar(Context context) {
super(context);
init();
}
public VersatileSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VersatileSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(mChangeHandler != null && mTextView != null){
mChangeHandler.onChange(i,mTextView);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
private void init(){
this.setOnSeekBarChangeListener(this);
}
public static abstract class ChangeHandler{
public abstract void onChange(int value,TextView textView);
}
}
致电代码:
myVersatileSeekBar.bindDisplayToChange(myTextView, new VersatileSeekBar.ChangeHandler() {
@Override
public void onChange(int value, TextView textView) {
textView.setText("level :" + value * 100);
}
});