将图标拖放到主屏幕

时间:2015-04-02 10:39:02

标签: java android view drag-and-drop layout-inflater

更新 要理解我的问题,这就是我需要实现的目标: 将图标从应用程序抽屉拖动到主屏幕(,如果可能不在网格视图中),如图中所示,

enter image description here

旧(这只是为了了解其工作原理):

我试图在同一个Activity或其他活动中实现从ListViewcustomView拖动可点击图标而没有容器(Listview or Gridview...),这是一张图片你要了解更多:

enter image description here

但是当我将图标放在右侧区域我没有看到对象时,在日志中我看到:I/ViewRootImpl﹕ Reporting drop result: true

enter image description here

这里是我的代码:

class MyDragListener implements View.OnDragListener {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            ...
            case DragEvent.ACTION_DROP:
                LinearLayoutAbsListView itemo = (LinearLayoutAbsListView)findViewById(R.id.paneko);
                View child = getLayoutInflater().inflate(R.layout.list_item, null);
                itemo.addView(child);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
             default:
                break;
        }
        return true;
    }
}

我的XML文件:

...
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:background="@android:color/background_dark"
    android:orientation="horizontal" >

    <com.moapps.elfassimounir.simple.LinearLayoutAbsListView
        android:id="@+id/paneuj"
        android:background="@android:color/background_light"
        android:orientation="vertical"
        >

        <ListView
            android:id="@+id/listview1"
            android:layout_width="100dp"
            android:layout_height="wrap_content" />
    </com.moapps.elfassimounir.simple.LinearLayoutAbsListView>

    <com.moapps.elfassimounir.simple.LinearLayoutAbsListView
        android:id="@+id/paneko"
        android:background="@android:color/background_light"
        android:orientation="vertical" >

    </com.moapps.elfassimounir.simple.LinearLayoutAbsListView>
</LinearLayout>

 ...

任何信息或参考资料(教程,文档......)都会非常有用

1 个答案:

答案 0 :(得分:2)

看一下向WindowManager(WM)添加视图。当您长按要拖动的项目时,创建自己的该项目的位图并将其添加到WM,以便可以在没有视图边界约束的情况下移动它。当您收到ACTION_UP或等效事件时,将当前的x,y映射到拖动项目正下方的实际视图(Rect类可能会有所帮助)。然后,您可以将此项目添加到该特定视图。

相关问题