0

就像均衡器将有多个搜索栏一样,我想实现多个搜索栏首选项,需要帮助。

我已经实现了单个搜索栏,不知道如何处理多个视图。

我的单一搜索栏偏好代码

public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPreference(context, attrs);
    }

    public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPreference(context, attrs);
    }

    private void initPreference(Context context, AttributeSet attrs) {
        setValuesFromXml(attrs);
        mSeekBar2 = new SeekBar(context, attrs);
        mSeekBar2.setMax(mMaxValue - mMinValue);
        mSeekBar2.setOnSeekBarChangeListener(this);
        mSeekBar1 = new SeekBar(context, attrs);
        mSeekBar1.setMax(mMaxValue - mMinValue);
        mSeekBar1.setOnSeekBarChangeListener(this);
    }

    private void setValuesFromXml(AttributeSet attrs) {
        mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
        mMinValue = attrs.getAttributeIntValue(ROBOBUNNYNS, "min", 0);

        mUnitsLeft = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsLeft",
                "");
        String units = getAttributeStringValue(attrs, ROBOBUNNYNS, "units", "");
        mUnitsRight = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsRight",
                units);

        try {
            String newInterval = attrs.getAttributeValue(ROBOBUNNYNS,
                    "interval");
            if (newInterval != null)
                mInterval = Integer.parseInt(newInterval);
        } catch (Exception e) {
            Log.e(TAG, "Invalid interval value", e);
        }

    }

    private String getAttributeStringValue(AttributeSet attrs,
            String namespace, String name, String defaultValue) {
        String value = attrs.getAttributeValue(namespace, name);
        if (value == null)
            value = defaultValue;

        return value;
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        RelativeLayout layout = null;

        try {
            LayoutInflater mInflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            layout = (RelativeLayout) mInflater.inflate(
                    R.layout.seek_bar_preference, parent, false);
        } catch (Exception e) {
            Log.e(TAG, "Error creating seek bar preference", e);
        }

        return layout;

    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);

        try {
            // move our seekbar to the new view we've been given
            ViewParent mSeekBar2oldContainer = mSeekBar2.getParent();
            ViewParent oldContainer = mSeekBar1.getParent();
            ViewGroup newContainer = (ViewGroup) view
                    .findViewById(R.id.seekBarPrefBarContainer);

            if (oldContainer != newContainer) {
                // remove the seekbar from the old view
                if (oldContainer != null) {
                    ((ViewGroup) oldContainer).removeView(mSeekBar1);
                    ((ViewGroup) oldContainer).removeView(mSeekBar2);
                }
                // remove the existing seekbar (there may not be one) and add
                // ours
                newContainer.removeAllViews();
                newContainer.addView(mSeekBar1,
                        ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                newContainer.addView(mSeekBar2,
                        ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        } catch (Exception ex) {
            Log.e(TAG, "Error binding view: " + ex.toString());
        }

        updateView(view);
    }

    /**
     * Update a SeekBarPreference view with our current state
     * 
     * @param view
     */
    protected void updateView(View view) {

        try {
            RelativeLayout layout = (RelativeLayout) view;

            mStatusText = (TextView) layout.findViewById(R.id.seekBarPrefValue);
            mStatusText.setText(String.valueOf(mCurrentValue));
            mStatusText.setMinimumWidth(30);

            mSeekBar1.setProgress(mCurrentValue - mMinValue);
            mSeekBar2.setProgress(mCurrentValue - mMinValue);

            TextView unitsRight = (TextView) layout
                    .findViewById(R.id.seekBarPrefUnitsRight);
            unitsRight.setText(mUnitsRight);

            TextView unitsLeft = (TextView) layout
                    .findViewById(R.id.seekBarPrefUnitsLeft);
            unitsLeft.setText(mUnitsLeft);

        } catch (Exception e) {
            Log.e(TAG, "Error updating seek bar preference", e);
        }

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        int newValue = progress + mMinValue;

        if (newValue > mMaxValue)
            newValue = mMaxValue;
        else if (newValue < mMinValue)
            newValue = mMinValue;
        else if (mInterval != 1 && newValue % mInterval != 0)
            newValue = Math.round(((float) newValue) / mInterval) * mInterval;

        // change rejected, revert to the previous value
        if (!callChangeListener(newValue)) {
            seekBar.setProgress(mCurrentValue - mMinValue);
            return;
        }

        // change accepted, store it
        mCurrentValue = newValue;
        mStatusText.setText(String.valueOf(newValue));
        persistInt(newValue);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        notifyChanged();
    }

    @Override
    protected Object onGetDefaultValue(TypedArray ta, int index) {

        int defaultValue = ta.getInt(index, DEFAULT_VALUE);
        return defaultValue;

    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

        if (restoreValue) {
            mCurrentValue = getPersistedInt(mCurrentValue);
        } else {
            int temp = 0;
            try {
                temp = (Integer) defaultValue;
            } catch (Exception ex) {
                Log.e(TAG, "Invalid default value: " + defaultValue.toString());
            }

            persistInt(temp);
            mCurrentValue = temp;
        }

    }

}
4

1 回答 1

0

您正在从构造函数调用此代码:

private void initPreference(Context context, AttributeSet attrs) {
    setValuesFromXml(attrs);
    mSeekBar2 = new SeekBar(context, attrs);
    mSeekBar2.setMax(mMaxValue - mMinValue);
    mSeekBar2.setOnSeekBarChangeListener(this);
    mSeekBar1 = new SeekBar(context, attrs);
    mSeekBar1.setMax(mMaxValue - mMinValue);
    mSeekBar1.setOnSeekBarChangeListener(this);
}

将它更好地移动到 onBindView 方法中:

@Override
protected void onBindView(View view) {
    super.onBindView(view);
    mSeekBar2 = new SeekBar(context, attrs);
    mSeekBar2.setMax(mMaxValue - mMinValue);
    mSeekBar2.setOnSeekBarChangeListener(this);
    mSeekBar1 = new SeekBar(context, attrs);
    mSeekBar1.setMax(mMaxValue - mMinValue);
    mSeekBar1.setOnSeekBarChangeListener(this);
}

使用自定义SeekBarPreference查看我的简单示例应用程序:

截屏

于 2015-05-14T11:11:24.043 回答