Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top