2

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

1 回答 1

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;
        }
    }
});
于 2013-04-17T02:29:51.930 回答