2011-12-05 39 views
0

我想拖放一个按钮在screen.i可以拖动,但是当我放下面的错误发生。 logcat的:拖放视图

12-05 15:03:00.905: E/AndroidRuntime(1009): java.lang.IllegalArgumentException: Given view not a child of [email protected] 
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> 

我想我必须更新可移动物体的当前视图bcoz其refrencing拖动层其父视图,但在我的XML按钮目前的观点是绝对布局。这错误是在onDrop()我提供了代码: - 。

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; 
} 

请帮那我应该怎么做才能克服这种issue..or如何处理按钮建议立即进行删除d不参考拖动它当前的父视图,但绝对布局..我知道什么问题,要么(1)在一个ViewGroup下移动所有可移动的视图有相同的父视图; (2)安排在视图被删除时更改正在移动的视图的父视图。但今天我想我会尝试我上面提出的建议:(3)在接受放置的视图中,制作被拖动视图的副本。将该副本附加为该视图的子项。回到原来的观点,并从其parent.But删除它如何代码来修复它,请帮我...感谢名单..

回答

1

只需使用此方法:

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); 
} 

你可以看到here 。而且,让你可以知道更多细节。