4

这里有很多关于 SO 的问题,要求提供一种方法来防止子视图复制其父母按下或选定的状态。但是,我在这里要求另一种方式:) - 我在我的一个应用程序中看到了一种非常奇怪的行为:

在 4.0.4 设备(API 15)上运行应用程序时,我看到的行为与明显的默认值相匹配,即:父级将其状态转发到其所有子视图。

当在更高的 API 级别(Android 4.4)上运行相同的应用程序而不进行任何更改时,此行为会发生变化:父级不会转发其状态。

duplicateParentState在 xml 布局中介绍了所有相关子视图,但这在这里似乎没有帮助。

这是一个已知的“问题”还是从 API 15 到 API >> 15 的计划行为变化?
如何确保在所有 API 级别正确转发状态?

如果它在这里有任何帮助/相关性:我要复制其父状态的子视图是ImageView添加 tintColors 的自定义 - 由于行为在 4.0.4 上是正确的,因此此类中不应有任何错误?

public class INCImageView extends ImageView {

    private int _tintColor;
    private int _highlightedTintColor;
    private int _selectedTintColor;

    public INCImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setFocusable(true);
        this.setClickable(true);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.INCImageView);

        _tintColor = array.getInt(R.styleable.INCImageView_tintColor, getResources().getColor(R.color.inc_tint));
        this.setColorFilter(_tintColor);

        _selectedTintColor = array.getInt(R.styleable.INCImageView_selectedTintColor, _tintColor);
        _highlightedTintColor = array.getInt(R.styleable.INCImageView_highlightedTintColor, _tintColor);

        array.recycle();
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);

        this.setColorFilter((selected ? _selectedTintColor : _tintColor));
    }

    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);

        this.setColorFilter((pressed ? _highlightedTintColor : (this.isSelected() ? _selectedTintColor : _tintColor)));
    }
}
4

1 回答 1

3

我找到了解决方案:

如果您查看ImageView上面的子类,在构造函数中clickable&focusable设置为true.

原来这是错误的。当孩子本身可点击时,父母不会转发其状态。- 这仍然不能解释为什么上面的代码在 4.0.4 上工作但在 4.4 上中断

无论如何,离开clickable & focusable = false解决了问题。

于 2014-12-01T23:34:45.737 回答