Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义首选项中的PersistInt减速按钮响应_Android_Android Preferences - Fatal编程技术网

Android 自定义首选项中的PersistInt减速按钮响应

Android 自定义首选项中的PersistInt减速按钮响应,android,android-preferences,Android,Android Preferences,我创建了一个嵌入两个按钮的自定义首选项(这里我将按钮子类化为FastButton)。问题是执行persistin来存储首选项会大大降低按钮的响应速度 我的想法是,只有在首选项的生命周期结束时才执行persistin,但找不到合适的方法来覆盖(即,对于首选项类,没有类似于onPause()的方法) 我尝试使用AsyncTask将persistInt移出UI线程时也失败了 关于如何减轻persistInt对UI响应的影响,有什么建议吗 public final class StepperPrefer

我创建了一个嵌入两个按钮的自定义首选项(这里我将按钮子类化为FastButton)。问题是执行persistin来存储首选项会大大降低按钮的响应速度

我的想法是,只有在首选项的生命周期结束时才执行persistin,但找不到合适的方法来覆盖(即,对于首选项类,没有类似于onPause()的方法)

我尝试使用AsyncTask将persistInt移出UI线程时也失败了

关于如何减轻persistInt对UI响应的影响,有什么建议吗

public final class StepperPreference extends Preference {

public int mCurrentValue = 1;
public int maxValue = Integer.MAX_VALUE;
public int minValue = Integer.MIN_VALUE;
private TextView mText;

private FastButton plusButton;
private FastButton minusButton;

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

public StepperPreference(Context context, AttributeSet attrs) {

    super(context, attrs);
    parseCustomAttributes(attrs);
}

public StepperPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    parseCustomAttributes(attrs);
}

public void setmCurrentValue(int value) {
    if (mCurrentValue != value) {
        mCurrentValue = value;
        persistInt(mCurrentValue);
    }
}

private void parseCustomAttributes(AttributeSet attrs) {

    int maxValueAttrInt=Integer.MAX_VALUE;
    int minValueAttrInt=Integer.MIN_VALUE;

    if (attrs!=null) {
        TypedArray a=getContext()
                .obtainStyledAttributes(attrs,
                        R.styleable.StepperPreference,
                        0, 0);

        maxValueAttrInt = a.getInt(R.styleable.StepperPreference_maxValue, Integer.MAX_VALUE);
        minValueAttrInt = a.getInt(R.styleable.StepperPreference_minValue, Integer.MIN_VALUE);
        a.recycle();
    }

    if (maxValueAttrInt > minValueAttrInt) {
        maxValue = maxValueAttrInt;
        minValue = minValueAttrInt;
    }
}

@Override
protected View onCreateView(ViewGroup parent) {
    LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = (View) li.inflate(R.layout.stepper_preference, parent, false);

    mText = (TextView) view.findViewById(R.id.text_view);
    Context context = getContext();

    int localDefaultValue = 0;
    mCurrentValue = getPersistedInt(localDefaultValue);
    mText.setText(Integer.toString(mCurrentValue));


    plusButton = (FastButton) view.findViewById(R.id.plus_button);
    plusButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (mCurrentValue < maxValue) {
                mCurrentValue++;
                mText.setText(Integer.toString(mCurrentValue));
                persistInt(mCurrentValue);

            }
        }
    });

    minusButton = (FastButton) view.findViewById(R.id.minus_button);
    minusButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (mCurrentValue > minValue) {
                mCurrentValue--;
                mText.setText(Integer.toString(mCurrentValue));
                persistInt(mCurrentValue);
            }
        }
    });

    return view;
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    int localDefaultValue = 0;
    Object result = a.getInt(index, localDefaultValue);
    return result;
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    int localDefaultValue = 0;
    setmCurrentValue(restoreValue ? this.getPersistedInt(localDefaultValue) : (int) defaultValue);
}

}
公共最终类StepperReference扩展了首选项{
公共int mCurrentValue=1;
public int maxValue=Integer.MAX_值;
public int minValue=Integer.MIN_值;
私有文本查看多行文本;
私人快捷按钮加上按钮;
私人快捷按钮;
公共步进引用(上下文){
超级(上下文);
}
公共步进引用(上下文、属性集属性){
超级(上下文,attrs);
属性(attrs);
}
公共步进引用(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
属性(attrs);
}
公共void setmCurrentValue(int值){
如果(mCurrentValue!=值){
mCurrentValue=值;
persistin(mCurrentValue);
}
}
私有void parseCustomAttributes(AttributeSet attrs){
int maxValueAttrInt=Integer.MAX_值;
int minValueAttrInt=Integer.MIN\u值;
如果(属性!=null){
TypedArray a=getContext()
.获得StyledAttributes(属性,
R.styleable.StepperPreference,
0, 0);
maxValueAttrInt=a.getInt(R.styleable.stepperReference\u maxValue,Integer.MAX\u VALUE);
minValueAttrInt=a.getInt(R.styleable.StepperPreference\u minValue,Integer.MIN\u VALUE);
a、 回收();
}
如果(maxValueAttrInt>minValueAttrInt){
maxValue=maxValueAttrInt;
minValue=minValueAttrInt;
}
}
@凌驾
受保护的视图onCreateView(视图组父级){
LayoutInflater li=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图=(视图)li.inflate(R.layout.stepper_首选项,父项,false);
mText=(TextView)view.findViewById(R.id.text\u视图);
Context=getContext();
int localDefaultValue=0;
mCurrentValue=getPersistedInt(localDefaultValue);
mText.setText(Integer.toString(mCurrentValue));
plusButton=(FastButton)view.findViewById(R.id.plus_按钮);
plusButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
如果(mCurrentValueminValue){
mCurrentValue--;
mText.setText(Integer.toString(mCurrentValue));
persistin(mCurrentValue);
}
}
});
返回视图;
}
@凌驾
受保护对象onGetDefaultValue(类型Darray a,整数索引){
int localDefaultValue=0;
对象结果=a.getInt(索引,localDefaultValue);
返回结果;
}
@凌驾
SetInitialValue上受保护的void(布尔恢复值,对象默认值){
int localDefaultValue=0;
setmCurrentValue(restoreValue?this.getPersistedInt(localDefaultValue):(int)defaultValue);
}
}

CheckBoxPreference
通过其
TwoStatePreference
超类,使用
persistBoolean()
保存首选项值,就像使用
persistInt()
一样。我认为在处理
复选框时没有明显的延迟。这意味着两件事之一:

  • 我是一个troglodyte,我看不到动画等方面明显的延迟


  • CheckBoxPreference
    不会显示您在
    stepperReference中看到的问题

  • 注:这两种可能性并不相互排斥

    如果我们假设#2是正确的,那么还有其他事情正在进行。方法跟踪,以查看您在
    persistin()
    的“下游”位置花费的时间,可能有助于确定
    StepperPreference
    的不同之处



    从你的评论来看,你有一个听众对偏好的变化做出反应,这就是导致反应迟钝的原因。“内联”首选项,如
    CheckBoxPreference
    StepperPreference
    ,将比
    DialogPreference
    子类(如
    ListPreference
    )更“烦躁”,这仅仅是因为更改首选项状态所需的工作更少(例如,一次屏幕点击与2次点击相比)。因此,听众需要更便宜。例如,在用户离开
    PreferenceFragment
    之前,您可能会推迟进行重要的工作,这样您就知道首选项值可能至少稳定一秒钟左右。

    Hmmm<当选中或取消选中
    复选框时,code>CheckBoxPreference
    (通过父级
    TwoStatePreference
    调用
    persistBoolean()
    ,因此您的方法应该是正常的。尝试使用方法跟踪来准确地查看您在哪里花费时间。如果我简单地注释掉persistin(),那么我会看到按钮响应速度显著加快。这让我相信persistin是我的瓶颈。哦,我不怀疑这一点。关键是:为什么
    persistin()
    对您如此有害,而它看起来不像
    CheckBoxPreference
    i