为什么不调用进度更改?

Why progress changed is not called?

我正在扩展 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" />

为什么回调进度一直不调用?

如果您希望 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);
    }
    ...

}