将Android父视图的“pressed”状态传递给子视图

4
在 StackOverflow 上有很多关于如何“防止”子视图复制其父视图按下或选定状态的问题。但是,我在这里问的是相反的问题:) 我在我的一个应用程序中看到了一种非常奇怪的行为:

当在 4.0.4 设备(API 15)上运行应用程序时,我看到的行为与表面上的默认值匹配,即:父级向所有子视图转发其状态。
当在更高的 API 级别(Android 4.4)上运行同一个应用程序且没有任何更改时,此行为会发生变化:父级不转发其状态。
我在 xml 布局中为所有相关子视图引入了 duplicateParentState,但这似乎并没有对此有所帮助。
这是已知的“问题”还是从 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)));
    }
}

能否将 super.set...setColorFilter 方法的顺序颠倒一下?这样应该可以工作。 - petey
是的,我可以反转它。- 经过测试,问题仍然存在 :( - CodingMeSwiftly
很遗憾,您能确认INCImageView的父级是否也是可聚焦和可点击的吗? - petey
是的 - imageView 的所有父级都是可聚焦和可点击的。 - CodingMeSwiftly
1个回答

4
我找到了解决方案:
如果您查看上面的ImageView子类,在构造函数中,clickable和focusable被设置为true。
结果证明这是错误的。当子项本身可点击时,父项不会转发其状态。- 这仍然无法解释为什么上述代码在4.0.4上能够正常工作,但在4.4上会出现问题。
无论如何,将clickable和focusable设置为false可以解决这个问题。

你只能在2天后接受自己编写的问题的答案。-.- - CodingMeSwiftly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接