Android:将项目拖出滚动列表/图库
我想实现一个允许用户将项目从中拖出的图库。这不应该妨碍滚动/滑动。
鉴于界面布局,用户只能以垂直路径将项目拖出图库,并水平滚动图库。
这可行吗?是否有一种简单的方法来检测水平运动,并将其传递给画廊的事件处理程序,并拦截垂直运动?或者我是否必须重写 onInterceptTouchEvent() 并自己进行数学计算?
(编辑:我正在尝试使用 GestureListener,重写 onFling 和 onScroll,并在垂直滚动距离低于阈值时将事件传递到图库)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我继承了Gallery,并重写了onScroll方法。我还没有实现放置逻辑,但是拖动和滚动可以工作。
当我有空的时候,我会在我的博客中写一篇完整的文章,其中包含更多细节和掉落机制。现在,只需进行简单的复制粘贴,以防将来有人访问此页面。
为了使行为保持在其所属的位置,我创建了这个 DraggableView 接口:
如果库中的视图实现了此视图,则可以将其拖出库区域。它们之前和之后都会收到通知,并且必须实现两个方法:
createDragView()
返回一个 DragView 对象。基本上,一个透明的悬停位图伴随着用户的移动。getDraggedInfo()
返回应到达放置目标的信息。这是 DragView 类:
如您所见,它在构造时需要一个 Bitmap,并创建一个悬停的 ImageView。最后,这里是(刚刚实现但不是很干净)画廊代码来实现这一切:
希望它有所帮助。
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
Bitmap
in 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.
我就是为了做到这一点而做的。这只是活动的代码...您需要一些布局和其他资源文件...
每个列表项都有一个随机匹配的图标和名称。
Here is something I did to do exactly that. That's only the code for the activity... there is some layout and other res files you'll need...
Every list item has an icon and name matched randomly.