我正在扩展SeekBar:
public class NutritionalSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
public NutritionalSeekBar(Context context) {
super(context);
}
public NutritionalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NutritionalSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NutritionalSeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = (Math.round(progress / 50)) * 50;
seekBar.setProgress(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
然后在xml:
中使用它<com.package.views.NutritionalSeekBar
android:id="@+id/seek_bar_carbos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:progress="50" />
为什么永远不会调用回调进度?
答案 0 :(得分:2)
如果您希望NutritionalSeekBar
实例成为其自己的OnSeekBarChangeListener
,则必须将其设置为此类。你也应该链接你的构造函数。例如:
public class NutritionalSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
public NutritionalSeekBar(Context context) {
this(context, null);
}
public NutritionalSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NutritionalSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnSeekBarChangeListener(this);
}
...
}