Gallery/AdapterView Child Drawable State

前端 未结 3 1680
心在旅途
心在旅途 2021-01-12 09:29

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

相关标签:
3条回答
  • 2021-01-12 10:12

    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.

    0 讨论(0)
  • 2021-01-12 10:19

    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 讨论(0)
  • 2021-01-12 10:28

    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.

    0 讨论(0)
提交回复
热议问题