我面临一个可能有人可以解决的问题 . 我需要 create a calendar and allow the user to select a date range by 1) clicking on a start date (this will create a 2 days range by default) and then 2) touching one range border and dragging it to increase or decrease that range .
目前我使用 RecyclerView with a GridLayoutManager 创建了所有日历天数单元格,其中包含很多行(所有周),每行7个项目(天) . 然后 I've used the RecyclerView ItemTouchHelper (https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.html) and ItemTouchHelper.Callback (https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.Callback.html) to complete the drag&drop functionality . 我的方法是使用那些助手提供的拖放检测(看看https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.ib8r012gc)修改它以避免移动一个项目并且只检测我想要的触摸拖放手势 . 我已经通过删除默认拖动动画(覆盖ItemTouchHelper onDraw / onDrawOver)并使用ItemTouchHelper.Callback回调来管理拖动动作(没有交换适配器项目,因为我总是需要它们在同一位置)来正确完成功能 . 一切正常,但 I'm now facing a problem because the drag detection is not working as expected . 我将尝试解释它: if I start the drag movement in the center of a given cell, and I move to the cell to its left, the drag movement isn't triggered until I've reached the center (在我到达另一个单元格的最左侧之前触发的位置相同的位置 .
现在 I'm trying to play a bit with the ItemTouchHelper.Callback.chooseDropTarget method 在手指进入新单元格时手动选择放置目标,但我无法使其工作 . 我认为(但我无法确定)接收到的x,y坐标有些奇怪 .
我知道一些代码示例将非常有用,但我无法粘贴它,因为它属于一个私有和受保护的存储库:(
Any help that anyone could provide to me will be really appreciated . 当然,我会尝试等待这个主题提供我能提供的所有信息 . 我还会考虑 new different approaches and suggestions to get the "touch+move+up" movement ,我需要移动范围边界:)
非常感谢所有Stack Overflow社区!
1 回答
经过一些研究,我找到了解决问题的方法 . 我会发布它,所以如果有人面临同样的问题,他可以在这里找到一些帮助:)
我的问题是我使用
ItemTouchHelper.Callback.chooseDropTarget
(https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.Callback.html#chooseDropTarget(android.support.v7.widget.RecyclerView.ViewHolder,%20java.util.List%3Candroid.support.v7.widget.RecyclerView.ViewHolder%3E,%20int,%20int))返回的curX
和curY
参数,就好像它们是拖动时的当前手指坐标一样,但它们是应用任何平移后拖动视图的最左上角坐标的坐标 . 现在我觉得有点傻 . 我应该更好地阅读文档:|由于我不需要最左上角坐标而是手指位置,我的解决方案是使用附加到源视图的触摸侦听器,这样我就可以使用
touchSrcX = MotionEvent.getX()
和touchSrcY = MotionEvent.getY()
检测初始触摸x,y坐标(相对于该视图)方法然后我在chooseDropTarget
回调中选择目标之前应用了点转换 . 我的意思是,像:curX += touchSrcX curY += touchSrcY
如果有人需要更多细节或更好的解释,请联系我,我很乐意为您提供帮助! :)