2012-04-27 38 views
3

我的自定義首選項類有助於顏色選擇。
自定義首選項調用GridView Intent,它包含9種顏色。
但是我的自定義類不能刷新顏色,這在GridView的

用戶選擇當我完成偏好的意圖,然後重新啓動偏好,
顏色顯示出來我,因爲我選擇的。Android自定義首選項改變覆蓋

onBindView()方法,我過度。這是完美的。
但我不知道我必須重寫刷新顏色的方法。

enter image description here


public class ConfigImage extends Preference 
{ 
    Context mContext; 

    public ConfigImage(Context context, AttributeSet attrs) 
    { 
     this(context, attrs, 0); 
     this.mContext = context; 
    } 

    public ConfigImage(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     this.mContext = context; 
     setLayoutResource(R.layout.preference_image); 
    } 

    @Override 
    public void onBindView(View view) 
    { 
     super.onBindView(view); 
     ImageView imageView = (ImageView) view.findViewById(R.id.preference_image_iv_color); 
     if (imageView != null) 
     { 
      SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mContext); 
      int colorIndex = sharedPref.getInt("setting_color_default", 3); 
      imageView.setBackgroundColor(Color.parseColor(ColorStorage.colors[colorIndex])); 
     } 
    } 
} 

<PreferenceCategory 
    android:key="setting_color_info" 
    android:title="색상" > 

    <com.pack.ConfigImage 
     android:id="@+id/setting_color_default" 
     android:key="setting_color_default" 
     android:selectable="true" 
     android:summary="Choose default color" 
     android:title="Default Color" 
     settings:background="#FFFFFFFF" /> 
</PreferenceCategory> 

回答

0

假設你的問題是沒有得到通知共享偏好的變化,那麼你應該訂閱它的變化像

registerOnSharedPreferenceChangeListener(this);