2014-05-20 83 views
0

就像均衡器將有多個搜索條一樣,我想要實現多個seekbarpreferences,需要幫助。如何在一個優先級中實現多個Seekbar首選

我已經實現了單個seekbar,不知道如何處理多個視圖。

我單seekbarpreference代碼

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; 
     } 

    } 

} 

回答

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

screenshot

相關問題