【问题标题】:Drag and Drop icons to Home Screen将图标拖放到主屏幕
【发布时间】:2015-06-07 06:51:10
【问题描述】:

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

旧的(这只是为了了解它是如何工作的):

我正在尝试实现将可点击图标从ListView 拖动到customView,在同一个Activity 或另一个Activity 中没有容器(Listview or Gridview...),这里有一张图片供您了解更多:

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

这是我的代码:

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>

 ...

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

【问题讨论】:

  • 这一行你在做什么 LinearLayoutAbsListView newParent = (LinearLayoutAbsListView)v; ,v是ID为pane3的视图吗?如果是这样,当没有为此视图定义列表视图时,您如何设置此视图的适配器?
  • 不,此代码与 Listview 一起使用只是为了让您了解我做了什么
  • LinearLayoutAbsListView 中的 v 是什么 newParent = (LinearLayoutAbsListView)v;关于你的 xml
  • 是的@random v 是ID为pane3的视图
  • 您能否确认passedItem 是否被添加到destList 并且newParent.absListView 是否引用了pane3 中的listview?

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


【解决方案1】:

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

【讨论】:

  • 嗨Jignesh,首先赞成并感谢您对此的解释,看来您已经使用过android启动器,现在我正在研究“launcher3”的代码源,我理解您的回答,但我可以'找不到他们创建位图并将其添加到 WM 的位置(类)(我正在寻找一个开始的示例)。顺便说一下 ACTION_UP... 和 Rect 类非常有帮助。
猜你喜欢
  • 1970-01-01
  • 2012-06-17
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
相关资源
最近更新 更多