下面是一个拖放示例:
https://www.tutlane.com/tutorial/android/android-drag-and-drop-with-examples
到目前为止,除了声明的错误“指定的子级已经有一个父级”之外,一切正常。必须首先对子级的父级调用removeview() DragEvent.ACTION_DROP
在 container.addView(vw)
. 我已经在这里使用了一个解决方案:
指定的子级已具有父级。必须首先对子级的父级调用removeview()(android)
并添加到抛出错误的行之前
if (vw.parent != null) {
val viewGroup = vw.parent as ViewGroup
viewGroup.removeView(vw)
}
和
container.removeAllViews()
但这似乎无法修复错误,以下是我的完整代码:
private fun setupDragDropMechanic() {
textView_test.tag = "DRAGGABLE TEXTVIEW"
textView_test.setOnLongClickListener(this)
layout1.setOnDragListener(this)
}
override fun onDrag(v: View?, event: DragEvent?): Boolean {
// Defines a variable to store the action type for the incoming event
when (event!!.action) {
DragEvent.ACTION_DRAG_STARTED -> {
// Determines if this View can accept the dragged data
return event.clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)
}
DragEvent.ACTION_DRAG_ENTERED -> {
v!!.background
.setColorFilter(
resources.getColor(R.color.colorToolbar),
PorterDuff.Mode.SRC_IN
)
// force a redraw
v.invalidate()
return true
}
DragEvent.ACTION_DRAG_LOCATION ->
// Ignored
return true
DragEvent.ACTION_DRAG_EXITED -> {
v!!.background.clearColorFilter()
v.invalidate()
return true
}
DragEvent.ACTION_DROP -> {
// Gets the item containing the dragged data
val item = event.clipData.getItemAt(0)
// Gets the text data from the item.
val dragData = item.text.toString()
Log.d("TAG", "data was dropped containing $dragData")
v!!.background.clearColorFilter()
v.invalidate()
val vw = event.localState as View
val owner = vw.parent as ViewGroup
owner.removeView(vw) //remove the dragged view
//caste the view into LinearLayout as our drag acceptable layout is LinearLayout
val container = v as LinearLayout
if (vw.parent != null) {
val viewGroup = vw.parent as ViewGroup
viewGroup.removeView(vw)
}
container.removeAllViews()
container.addView(vw) //Add the dragged view
vw.visibility = View.VISIBLE //finally set Visibility to VISIBLE
// Returns true. DragEvent.getResult() will return true.
// remove listener from view so user can't drop again
v.setOnDragListener(null)
// remove listener from dragged view
vw.setOnLongClickListener(null)
v.invalidate()
return true
}
DragEvent.ACTION_DRAG_ENDED -> {
if (event.result) {
Log.d("TAG", "Drop handled successful")
} else {
Log.d("TAG", "Drop failed to handle")
}
return true
}
else -> Log.d("hh", "Unknown action type received by OnDragListener")
}
return false
}
override fun onLongClick(v: View?): Boolean {
// Create a new ClipData.Item from the ImageView object's tag
val item = ClipData.Item(v!!.tag as CharSequence)
// Create a new ClipData using the tag as a label, the plain text MIME type, and
// the already-created item. This will create a new ClipDescription object within the
// ClipData, and set its MIME type entry to "text/plain"
val mimeTypes = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
val data = ClipData(v.tag.toString(), mimeTypes, item)
// Instantiates the drag shadow builder.
val dragshadow = DragShadowBuilder(v)
// Starts the drag
v.startDrag(
data // data to be dragged
, dragshadow // drag shadow builder
, v // local data about the drag and drop operation
, 0 // flags (not currently used, set to 0)
)
return true
}
以及我的xml布局:
<!-- DRAG DROP -->
<TextView
android:id="@+id/textView_test"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="center"
android:background="@drawable/rounded_border"
android:layout_below="@+id/linearLayout_button_hide_show_question"
android:text="Das ist mein Text zum ziehen"/>
<LinearLayout
android:id="@+id/layout1"
android:layout_width="200dp"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#00ADEF"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"/>
1条答案
按热度按时间46scxncf1#
我修正了错误。布局被 Package 在相对布局中,dragdrop无法处理此布局。所以我把它搬出了真实的布局,效果很不错