如何将选定的OnItemLongClick从一个ViewPager项拖到另一个 [英] How to drag selected OnItemLongClick from one ViewPager item to another
问题描述
所以我有一个带有条形片段(ListFragment)和团队片段(ListFragment)的FragmentPagerAdapter。当一个项目被长按时,我希望能够从团队选项卡中拖动它并将其放在栏标签上以建立关系。这在理论上与主屏幕类似,如果我靠近边缘,我可以到达下一个屏幕。
So I have a FragmentPagerAdapter with a bar fragment (ListFragment) and a team fragment (ListFragment). When an item is long-clicked I want to be able to drag it from the teams tab and drop it on the bars tab to make a relationship. This would theoretically work similar to the home screen, where if I get close to the edge with the item I can got to the next screen.
更新:
我使用3.0+或兼容性库。
I am using 3.0+ or compatibility library.
我试图拖到另一个选项卡上,而不是在同一个选项卡中。
I am trying to drag onto ANOTHER TAB, not within the same tab.
推荐答案
这取决于您所定位的级别。如果您的应用程序设计为在Android 3.0或更高版本上运行,则可以定位内置的拖放API 。
It depends on the level you are targeting. If your application is designed to run on Android 3.0 or later, you can target the built-in Drag and Drop API.
如果您的应用程序需要定位早期版本,这是一个很好的开源库( Github页面)我之前使用过,并尝试为2.0或更高版本的应用程序镜像内置API的功能。
If your application needs to target an earlier version, this is a good open-source library (Github page) thatI I've used before and tries to mirror the functionality of the built-in API for apps targeting 2.0 or later.
在任何一种情况下,你都可以从 onItemLongClick()
开始使用 startDrag()
进行拖动操作回调。
In either case, you would start a drag operation with startDrag()
from the onItemLongClick()
callback.
修改:抱歉造成混淆
您必须以编程方式根据 DragEvent
数据执行 ViewPager
中的标签更改:
You would have to programmatically do the "tab" changes in your ViewPager
based on the DragEvent
data:
- 由于
ViewPa ger
只是支持库的一部分(并没有向后移植),您可能需要将OnDragEventListener
附加到其父ViewGroup。 - 您可以在
ACTION_DRAG_LOCATION
事件触发监听器时监控拖动位置。定义一个合理的阈值,手指必须在侦听视图的边缘内到达,当它到达那里时,在ViewPager <上调用
setCurrentItem()
/ code>要切换。 - 只要在开始拖动时在视图层次结构中实例化了新选项卡(这由
setOffscreenPageLimit控制( )
onViewPager
),新页面上的视图也应该是放置目标的候选者,所以他们应该能够听取ACTION_DROP
event。
- Since
ViewPager
is only part of the support library (and was not backported), you will probably have to attach anOnDragEventListener
to its parent ViewGroup. - You can monitor the drag location while the
ACTION_DRAG_LOCATION
events fire to the listener. Define a reasonable threshold that the finger must reach within the edge of the listening View and, when it gets there, callsetCurrentItem()
on theViewPager
to switch. - As long as your new tab was instantiated in the view hierarchy when you started the drag (this is controlled by
setOffscreenPageLimit()
onViewPager
), the views on the new page should also be candidates for a drop target, so they should be able to listen for theACTION_DROP
event.
HTH
这篇关于如何将选定的OnItemLongClick从一个ViewPager项拖到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!