图库/AdapterView 子可绘制状态

发布于 2024-09-26 05:16:46 字数 293 浏览 4 评论 0原文

我正在使用图库视图,其中与每个项目相对应的视图都非常重要,并且由文本和多个按钮组成。

当我单击以拖动图库视图(不在按钮之一上的某处)时,按钮的可绘制状态更改为按下,并且看起来好像所有按钮当前都被按下。此外,选定状态也会发生相同的行为(例如,子 TextView 的所有文本都会更改颜色)。

我试图阻止这种行为,并找到了 android:duplicateParentState xml 属性以及 setDuplicateParentStateEnabled 属性。这似乎应该完成我想做的事情,但似乎没有效果。

有什么想法吗?

I am using a Gallery view where the view corresponding to each item is non-trivial and consists of text as well as multiple buttons.

When I click to drag the gallery view (somewhere not on one of the buttons) the button's drawable state changes to pressed and appears as if all of the buttons are currently being pressed. Additionally, the same behavior happens for the selected state (e.g. all of the text of the child TextViews changes color).

I am trying to prevent this behavior and have found the android:duplicateParentState xml attribute as well as the setDuplicateParentStateEnabled property. This seems like it should accomplish what I am trying to do, but it seems to have no effect.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风追烟花雨 2024-10-03 05:16:46

也许有点晚了,但最近我自己也遇到了这个问题,我已经解决了。

dispatchSetSelected 和dispatchSetPressed 在这里并没有真正的帮助,因为您实际需要重写的方法在Gallery 中是私有的。

解决方案是在子视图周围添加一个额外的布局(我使用了 LinearLayout 的子类),它覆盖了 setPressed(boolean) 并简单地忽略它。这可以防止画廊将其按下状态强加给子级,同时仍然允许直接激活这些子级,并且不会阻止事件向上传播。

Maybe a bit on the late side, but having run into this problem myself recently I have a fix.

dispatchSetSelected and dispatchSetPressed don't really help here, as the methods you actually need to override are private within Gallery.

The solution is to add an extra layout (I used a subclass of LinearLayout) around the child view, which overrides setPressed(boolean) and simply ignores it. This prevents the gallery from forcing its pressed state on the children, while still allowing those children to be activated directly, and not stopping events from being propagated upwards.

久光 2024-10-03 05:16:46

不确定duplicateParentState属性在哪里发挥作用,但突破源代码表明它从未被考虑到选定和按下状态。我会创建一个自定义视图类并重写dispatchSetSelected 和dispatchSetPressed 方法。

Not sure where the duplicateParentState property comes into play, but breaking through the source shows that it is never taken into account for the selected and pressed states. I would up creating a custom view class and overriding the dispatchSetSelected and dispatchSetPressed methods.

美男兮 2024-10-03 05:16:46

如果您碰巧从图库扩展来创建您自己的自定义版本,您可以简单地执行以下操作:

  @Override
  public boolean onDown(MotionEvent e) {
    // Ignore onDown events in order to avoid having every child's state set to 'pressed'
    return true;
  }

这仍然允许自定义图库的子项正确接收单击事件,并按预期滑动/投掷工作。

If you happen to extend from Gallery to create your own custom version of it, you can simply do:

  @Override
  public boolean onDown(MotionEvent e) {
    // Ignore onDown events in order to avoid having every child's state set to 'pressed'
    return true;
  }

This will still allow the custom gallery's children to properly receive click events, and swiping / flinging works as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文