我正在使用Range Seek Bar并且我已经有效地初始化了日期范围滑块。如何将滑块设置为具有10年的最小范围差异?我的滑块是1950年至今。我只想允许年龄差异最小的范围。
这就是我最终做的事情:
@Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Long minValue, Long maxValue) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy", Locale.US);
int minYear = Integer.parseInt(formatter.format(minValue));
int maxYear = Integer.parseInt(formatter.format(maxValue));
//Log.i(getClass().getName(), "User selected new range values: MIN=" + minYear + ", MAX=" + maxYear);
if ((maxYear - minYear) < 10) {
//range is less than 10 years so we have to manually set it back.
if (minValue != previousMin){
//min is what changed
mDateRangeSeekBar.setSelectedMinValue(previousMin);
} else if (maxValue != previousMax) {
//max is what changed
mDateRangeSeekBar.setSelectedMaxValue(previousMax);
}
} else {
//range was big enough so set previous values for next time.
previousMin = minValue;
previousMax = maxValue;
}
mMinDateTemp = new Date(mDateRangeSeekBar.getSelectedMinValue());
mMaxDateTemp = new Date(mDateRangeSeekBar.getSelectedMaxValue());
refresh();
}
答案 0 :(得分:2)
您应该能够使用onChanged()回调来检查范围的值,如果它太小,则设置更改为前一个值的值。
这样的东西(我没有编译,所以你可能不得不修复错字或小逻辑错误):
//declare these in your activity
private int previousMin = -1;
private int previousMax = -1;
seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
@Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
// handle changed range values
Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
if((maxValue - minValue) < 10){
//range is less than 10 so we have to manually set it back.
if(minValue != previousMin){
//min is what changed
seekBar.setSelectedMinValue(previousMin);
}
else if(maxValue != previousMax){
//max is what changed
seekBar.setSelectedMaxValue(previousMax);
}
}else{
//range was big enough so set previous values for next time.
previousMin = minValue;
previousMax = maxValue;
}
}
});