I want to implement a Gallery that allows the user to drag items out of it. This shouldn’t get in the way of scrolling/flinging.
Given the interface layout, the user can only drag items out of the Gallery in a vertical path, and scroll the Gallery horizontally.
Is this feasible? Is there an easy way of detecting horizontal movements, and defer them to the Gallery’s event handlers, and intercept vertical movements? Or do I have to override onInterceptTouchEvent() and do the math myself?
(edit: I’m giving a try to a GestureListener, overriding onFling and onScroll, and passing the events to the Gallery when the vertical scroll distance is below a threshold)
I inherited Gallery, and overrode the onScroll method. I haven’t implemented the drop logic yet, but the dragging and scrolling work.
When I can spare the time, I’ll write a full post in my blog with more details, and the drop mechanism. For now, a simple copy-paste in case somebody reaches this page in the future.
To keep the behavior where it belongs, I created this DraggableView interface:
Views in the Gallery can be dragged out of the Gallery area if they implement this view. They are notified before and after, and must implement two methods:
createDragView()returns a DragView object. Basically, a transparent hovering bitmap to accompany the user’s movement.getDraggedInfo()returns the information that should reach the drop target.Here’s the DragView class:
As you can see, it takes a
Bitmapin construction, and creates a hoveringImageView. Finally, here is the (just implemented and not very clean) Gallery code to make it all happen:Hope it helps.