0

I have a viewgroup with textviews that are added dynamically into the viewgroup. How would I add the ability to drag and drop a textview between other textviews.

drag-and-drop-android-question

Is there a way to detect what view you have dropped it over.

If it makes it easier the textviews could be buttons.

4

1 回答 1

2

在 API 11 及更高版本上,您可以使用内置功能https ://developer.android.com/guide/topics/ui/drag-drop.html

在以前的版本中,您也可以手动完成

理论上,您必须为您的视图添加一个 onTouchListener,它会在向下操作时保存相对于视图位置的触摸位置。

然后,在接收到触摸移动事件时,您设置视图的位置以反映这一点。

在接收到 up 事件时,用户已放下视图,您检查当前坐标并将它们与其他视图进行比较,如果它在另一个视图之前,则将该视图向上移动并将移动视图的位置设置为与其他人再次。

您可以通过仅更改这些值将拖动限制为仅 x/仅 y。

于 2012-11-23T22:39:43.537 回答