开发者

Gallery/AdapterView Child Drawable State

开发者 https://www.devze.com 2023-01-18 13:12 出处:网络
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.

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 开发者_JAVA百科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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消