Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

android - Drag and drop a view

I am trying to drag and drop a button on the screen.i can drag but when i drop the following error takes place. Logcat:

12-05 15:03:00.905: E/AndroidRuntime(1009): java.lang.IllegalArgumentException: Given view not a child of com.avigma.learning.DragLayer@44f5eda0
12-05 15:03:00.905: E/AndroidRuntime(1009):     at android.view.ViewGroup.updateViewLayout(ViewGroup.java:1876)
12-05 15:03:00.905: E/AndroidRuntime(1009):     at com.avigma.learning.DragLayer.onDrop(DragLayer.java:131)
12-05 15:03:00.905: E/AndroidRuntime(1009):     at com.avigma.learning.DragController.drop(DragController.java:447)
12-05 15:03:00.905: E/AndroidRuntime(1009):     at com.avigma.learning.DragController.onTouchEvent(DragController.java:424)
12-05 15:03:00.905: E/AndroidRuntime(1009):     at com.avigma.learning.DragLayer.onTouchEvent(DragLayer.java:69)

xml:-

<?xml version="1.0" encoding="utf-8"?>
<com.avigma.learning.DragLayer
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"

    android:id="@+id/drag_layer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


  <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
       android:src="@drawable/b" />

  <ScrollView
      android:id="@+id/scrollView1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_x="-4dp"
      android:layout_y="2dp" >


      <AbsoluteLayout
          android:id="@+id/ll"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >

   //This button i am dragging and dropping
<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="84dp"
    android:layout_y="90dp"
    android:background="@null"
    android:text="B"
    android:textColor="#000000"
    android:textSize="35dp"
    android:textStyle="bold"
    android:typeface="serif" />

</AbsoluteLayout>


  </ScrollView>

</AbsoluteLayout>
</com.avigma.learning.DragLayer>

I think i have to update the current view of movable object bcoz its refrencing drag layer its parent view but in my xml button current view is absolute layout.this error is in onDrop().I am providing its code:-

 private boolean drop(float x, float y) {

    final int[] coordinates = mCoordinatesTemp;
    DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);

    if (dropTarget != null) {
        dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
                (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
        if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
                (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {

            dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
                    (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
            mDragSource.onDropCompleted((View) dropTarget, true);
            return true;
        } else {
            mDragSource.onDropCompleted((View) dropTarget, false);
            return true;
        }
    }
    return false;
}

please help that what should i do to overcome this issue..or how to handle that button should not refrence drag layer its current parent view but absolute layout..I know whats the problem that either(1) move everything under one ViewGroup so all the movable views have the same parent view; (2) arrange to change the parent view of the view being moved when that view is dropped. But today I think I’d try what I suggested above: (3) in the view that is accepting the drop, make a copy of the view being dragged. Attach the copy as a child of that view. Go back to the original view and remove it from its parent.But how to code that to fix it please assist me...thanx..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply use this method:

public interface DropListener {

/**
 * Called when an item is to be dropped.
 * @param from - index item started at.
 * @param to - index to place item at.
 */
void onDrop(int from, int to);
}

You can see here. And, let you can know more detail.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...