我正在使用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();
}