2010-10-13 1046 views
1

我写了一个自定义首选项类,其中包含一个简单的进度栏,以允许设置音量等。 偏好效果很好,直到我尝试将这两个偏好设置放入我的首选项屏幕中。自定义首选项跳过屏幕

当我这样做时,首选项屏幕开始变得很粗糙»当我拖动搜索栏时,两个自定义首选项会不断切换位置。

任何人有任何想法为什么发生这种情况? 感谢

编辑: 这里是习俗偏好的代码

import android.content.Context; import 

android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Typeface; import android.preference.Preference; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; 

public class SeekBarPreference extends Preference implements  OnSeekBarChangeListener { 


    private int mMaximum = 60; private int mInterval = 1; private int mMinimum = 1; 

    private int mDefault = 50; private TextView monitorBox; 

    public SeekBarPreference(Context context) {  super(context);  } 

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

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

    private void initValues(Context context, AttributeSet attrs) {  TypedArray styledAttributes = context.obtainStyledAttributes(attrs, 
       R.styleable.SeekBarPreference);   mMinimum = styledAttributes.getInt(
       R.styleable.SeekBarPreference_barMinValue, 1);  mMaximum = styledAttributes.getInt(
       R.styleable.SeekBarPreference_barMaxValue, 1);  mInterval = styledAttributes.getInt(
       R.styleable.SeekBarPreference_barInterval, 1);  mDefault = styledAttributes.getInt(
       R.styleable.SeekBarPreference_defaultValue, mMaximum); } 

    @Override protected View onCreateView(ViewGroup parent) { 

     LinearLayout horizontalLayout = new LinearLayout(getContext());   horizontalLayout.setPadding(15, 5, 10, 5);  horizontalLayout.setOrientation(LinearLayout.HORIZONTAL); 

     LinearLayout verticalLayout = new LinearLayout(getContext());  verticalLayout.setOrientation(LinearLayout.VERTICAL); 

     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT);  layoutParams.gravity = Gravity.LEFT;  layoutParams.weight = 0.5f; 

     LinearLayout.LayoutParams settingTitleLayoutParams = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT);  settingTitleLayoutParams.gravity = Gravity.LEFT;  settingTitleLayoutParams.weight = 
1.0f; 

     LinearLayout.LayoutParams seekbarLayoutParams = new LinearLayout.LayoutParams(
       80, LinearLayout.LayoutParams.WRAP_CONTENT);  seekbarLayoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL; 

     LinearLayout.LayoutParams selectedAmountLayoutParams = new LinearLayout.LayoutParams(
       30, LinearLayout.LayoutParams.WRAP_CONTENT);  selectedAmountLayoutParams.gravity = Gravity.CENTER; 

     TextView titleTextView = new TextView(getContext());  titleTextView.setText(getTitle());  titleTextView.setTextSize(18);  titleTextView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);  titleTextView.setGravity(Gravity.LEFT);   titleTextView.setLayoutParams(settingTitleLayoutParams); 

     TextView summeryTextView = new TextView(getContext());  summeryTextView.setText(getSummary());  summeryTextView.setTextSize(14);  summeryTextView.setTypeface(Typeface.SANS_SERIF);  summeryTextView.setGravity(Gravity.LEFT);  summeryTextView.setLayoutParams(settingTitleLayoutParams); 

     SeekBar bar = new SeekBar(getContext());  bar.setMax(mMaximum);   bar.setProgress(mDefault);  bar.setPadding(10, 0, 10, 0);  bar.setLayoutParams(seekbarLayoutParams);  bar.setOnSeekBarChangeListener(this); 

     this.monitorBox = new TextView(getContext());  this.monitorBox.setTextSize(12);  this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);  this.monitorBox.setLayoutParams(selectedAmountLayoutParams);  this.monitorBox.setPadding(2, 5, 0, 0);   this.monitorBox.setText(bar.getProgress() 
+ ""); 

     verticalLayout.addView(titleTextView);  verticalLayout.addView(summeryTextView);  horizontalLayout.addView(verticalLayout, layoutParams);   horizontalLayout.addView(bar);  horizontalLayout.addView(this.monitorBox);  horizontalLayout.setId(android.R.id.widget_frame); 

     return horizontalLayout; } 

    public void onProgressChanged(SeekBar seekBar, int progress,   boolean fromUser) { 

     progress = Math.round(((float) progress)/mInterval) * mInterval;  if (progress < mMinimum)  {   progress = mMinimum;  } 

     if (!callChangeListener(progress))  {   seekBar.setProgress((int) this.mDefault);   return;   } 

     seekBar.setProgress(progress);  this.mDefault = progress;  this.monitorBox.setText(progress + "");   updatePreference(progress); 

     notifyChanged(); } 

    public void onStartTrackingTouch(SeekBar seekBar) { } 

    public void onStopTrackingTouch(SeekBar seekBar) { } 

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

     int dValue = (int) ta.getInt(index, 5); 

     return validateValue(dValue); } 

    @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue)  {  int temp = restoreValue ? getPersistedInt(5) : (Integer) defaultValue; 

     if (!restoreValue)   persistInt(temp); 

     this.mDefault = temp; } 

    private int validateValue(int value) { 

     if (value > mMaximum)   value = mMaximum;  else if (value < 0)    value = 0;  else if (value % mInterval != 0)   value = Math.round(((float) value)/mInterval) * mInterval; 

     return value; } 

    private void updatePreference(int newValue)  { 

     SharedPreferences.Editor editor = getEditor();  editor.putInt(getKey(), newValue);  editor.commit(); } 

} 

编辑:我发现,创造与excat相同的代码,但不同名称的第二类每一个使用一个解决问题=有史以来最糟糕的一次。我不想这样做。任何人有任何想法?

+0

发布代码! – Nate 2010-10-13 22:11:31

+0

在这里发布的文件更容易redabilty:http://jump.fm/EUNTP – Jason 2010-10-15 15:00:17

回答

2

不是一个真正的解决方案,但定义您的布局为XML将导致更好更快的程序。您可能会在途中解决您的问题。毋庸赘言,如果你的黑客入侵在一起,它没有错误的做它programaticaly。这就是说...


你可以试试服用的是从左到右的元素并将其放置在一个线性视图和水平设置它,然后堆积这些元素变成垂直线性图。看起来,并排浮动的意见是导致电话,使酒吧来回跳。一个经典的CSS问题(我正在交换浮动和重力)

我认为发生了什么是当你更新搜索栏手机正在更新视图并针对浮动视图的位置进行不同的计算。如果你使用垂直和水平的线性视图放置一切,它应该解决你的问题,并使你的布局更简单。

+0

谢谢,我会在早上尝试这个,我已经筋疲力尽了。 – Jason 2010-10-28 21:05:58

0

我猜测这是一个内联首选项,就像CheckboxPreference,而不是像EditTextPreference那样的DialogPreference

如果是这样,看看你的代码,并确保你不会无意中在同一个首选项的多个实例之间共享内容。我只做了DialogPreferences,所以我不太确定如何设置内联首选项。但是,如果,比如说你打电话给findViewById()或者其他什么来获得你的SeekBar,我可以看到你的两个偏好可能会意外地交织在一起。或者,如果您使用的是可变静态数据成员,那么我可能会看到潜在的问题。

除此之外,正如布罗斯先生所建议的那样,很难在没有看到代码的情况下给出建议。

+0

谢谢你会检查出来,当我回家。如果它有帮助,它也会发布代码。 – Jason 2010-10-14 05:26:08

+0

好吧,我找不到像你提到的任何东西 - 发布代码也许这将有所帮助。 – Jason 2010-10-15 14:51:48

+0

@Jason:对不起,没有任何东西可以作为你困难的根源。 – CommonsWare 2010-10-15 15:10:06