拖放一个视图 [英] Drag and drop a view

查看:102
本文介绍了拖放一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:-

<?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按钮,目前看来是绝对的layout.this误差在onDrop()。我提供了code : -

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

请帮忙说我应该怎么做才能克服这个issue..or如何处理按钮不应refrence拖动层当前父视图,但绝对layout..I知道什么问题,要么(1)把一切都之一的, ViewGroup中,因此所有活动的意见具有相同的父视图; (2)安排更改视图的父视图被移动时,这一观点被丢弃。但今天我想我会尽我上面的建议:(3)在正在接受下降的观点,使视图的副本被拖动。附加的副本作为这一观点的子项。回到原来的观点,并从其parent.But删除它如何code,要解决这个问题,请帮助我...感谢名单..

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..

推荐答案

只需使用此方法:

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.

这篇关于拖放一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆