自己写的SeekBarPreference,可以实现seekbar滑动监听和设置默认进度和最大进度

自己写的SeekBarPreference,可以实现seekbar滑动监听和设置默认进度和最大进度

我通过参考android源码,把这个烂尾的类写完了。具体实现了seekbar的数据自动存储,seekbar拖动时触发监听器,可以设置默认的进度和最大进度。先说使用的方式:

1.在xml文件中使用preference

<com.kale.floatbar.preference.MySeekBarPreference
android:key="alpha"
android:layout="@layout/prefs_list_item"
android:title="透明度" />

2,在Java代码中找到preference然后设置数值或绑定监听器

      private MySeekBarPreference alphaSb
        alphaSb = (MySeekBarPreference) findPreference("alpha");//找到preference
alphaSb.setDefaultProgressValue(100);//设置起始时的进度
alphaSb.setMax(255);//设置最大的数值,不超过10000。如果超过了请在seekbarPreference源码中进行修改max值
alphaSb.setOnSeekBarPrefsChangeListener(this);//设置监听器

3.设置监听器,这里的key是当前触发监听器的preference的key,这样可以区分是哪个seekbarPreference触发的监听器

    @Override
public void onStopTrackingTouch(String key, SeekBar seekBar) {
} @Override
public void onStartTrackingTouch(String key, SeekBar seekBar) {
} @Override
public void onProgressChanged(String key, SeekBar seekBar, int progress, boolean fromUser) { }

最后是SeekbarPreference的源码:

/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kale.floatbar.preference; import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView; import com.kale.floatbar.R; public class MySeekBarPreference extends Preference implements OnSeekBarChangeListener{ private TextView value;
private int mProgress;
private int mMax = 10000;//如果您的seekbar最大值超过了10000,那么在这里修改下即可。否则会被限制最大值为10000
private boolean mTrackingTouch;
private OnSeekBarPrefsChangeListener mListener = null; public MySeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); setMax(mMax);
setLayoutResource(R.layout.seekbar_prefs);
} public MySeekBarPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public MySeekBarPreference(Context context) {
this(context, null);
} @Override
protected void onBindView(View view) {
super.onBindView(view); SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
seekBar.setMax(mMax);
seekBar.setProgress(mProgress);
seekBar.setEnabled(isEnabled());
seekBar.setOnSeekBarChangeListener(this);
value = (TextView)view.findViewById(R.id.value);
value.setText(String.valueOf(mProgress));
} /** 设置默认的值
* @param defaultValue
*/
public void setDefaultProgressValue(int defaultValue) {
if(getPersistedInt(-1) == -1) {
//说明没有用户设定的值,所以设置初始值
setProgress(defaultValue);
}
} @Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setProgress(restoreValue ? getPersistedInt(mProgress): (Integer) defaultValue);
} public void setMax(int max) {
if (max != mMax) {
mMax = max;
notifyChanged();
}
} public void setProgress(int progress) {
setProgress(progress, true);
} private void setProgress(int progress, boolean notifyChanged) {
if (progress > mMax) {
progress = mMax;
}
if (progress < 0) {
progress = 0;
}
if (progress != mProgress) {
mProgress = progress;
persistInt(progress);
if (notifyChanged) {
notifyChanged();
}
}
} public int getProgress() {
return mProgress;
} public void setOnSeekBarPrefsChangeListener(OnSeekBarPrefsChangeListener listener) {
mListener = listener;
} /**
* @author:Jack Tony
* @tips :设置监听器
* @date :2014-8-17
*/
public interface OnSeekBarPrefsChangeListener {
//public void OnSeekBarChangeListener(SeekBar seekBar, boolean isChecked);
public void onStopTrackingTouch(String key ,SeekBar seekBar) ;
public void onStartTrackingTouch(String key ,SeekBar seekBar);
public void onProgressChanged(String key ,SeekBar seekBar, int progress,boolean fromUser);
} /**
* Persist the seekBar's progress value if callChangeListener
* returns true, otherwise set the seekBar's progress to the stored value
*/
void syncProgress(SeekBar seekBar) {
int progress = seekBar.getProgress();
if (progress != mProgress) {
if (callChangeListener(progress)) {
setProgress(progress, false);
} else {
seekBar.setProgress(mProgress);
}
}
} @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//value.setText(seekBar.getProgress()+"");
if (mListener != null) {
mListener.onProgressChanged(getKey(),seekBar, progress, fromUser);
}
if (seekBar.getProgress() != mProgress) {
syncProgress(seekBar);
}
if (fromUser && !mTrackingTouch) {
syncProgress(seekBar);
}
} @Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (mListener != null) {
mListener.onStartTrackingTouch(getKey(),seekBar);
}
mTrackingTouch = true;
} @Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mListener != null) {
mListener.onStopTrackingTouch(getKey(),seekBar);
}
mTrackingTouch = false;
if (seekBar.getProgress() != mProgress) {
syncProgress(seekBar);
}
notifyHierarchyChanged();
} @Override
protected Parcelable onSaveInstanceState() {
/*
* Suppose a client uses this preference type without persisting. We
* must save the instance state so it is able to, for example, survive
* orientation changes.
*/ final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
return superState;
} // Save the instance state
final SavedState myState = new SavedState(superState);
myState.progress = mProgress;
myState.max = mMax;
return myState;
} @Override
protected void onRestoreInstanceState(Parcelable state) {
if (!state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
} // Restore the instance state
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
mProgress = myState.progress; mMax = myState.max;
notifyChanged();
} /**
* SavedState, a subclass of {@link BaseSavedState}, will store the state
* of MyPreference, a subclass of Preference.
* <p>
* It is important to always call through to super methods.
*/
private static class SavedState extends BaseSavedState {
int progress;
int max; public SavedState(Parcel source) {
super(source);
// Restore the click counter
progress = source.readInt();
max = source.readInt();
} @Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Save the click counter
dest.writeInt(progress);
dest.writeInt(max);
} public SavedState(Parcelable superState) {
super(superState);
} }
}
上一篇:SpringCloud Stream生产者配置RabbitMq的动态路由键


下一篇:爬虫基础(1):urllib库