2015-04-02 183 views
8

更新: 明白我的問題,這是我需要實現: 拖動圖標從應用程序抽屜主屏幕(如果可能的話沒有在GridView)像PIC,拖放圖標到主屏幕

enter image description here

舊(這只是爲了學習如何工作):

我試圖實現從拖動點擊圖標一個ListView與同一活動內沒有容器(Listview or Gridview...)一customView或其他,這裏是一個畫面讓你瞭解更多:

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> 

... 

任何相關信息或引用(教程,文檔...)將是非常有益

+0

你在做什麼這一行LinearLayoutAbsListView newParent =(LinearLayoutAbsListView)v; ,是V與ID pane3視圖?如果是這樣的話,當沒有爲這個視圖定義listview時,你如何爲這個視圖設置適配器? – random 2015-04-02 11:02:54

+0

沒有這個代碼與Listview只是爲了讓你知道我做了什麼 – 2015-04-02 11:14:29

+0

再次什麼是LinearLayoutAbsListView中的v newParent =(LinearLayoutAbsListView)v;關於你的xml – random 2015-04-02 11:15:49

回答

2

看一看添加視圖的窗口管理器(WM )。當您長時間按住要拖動的項目時,請爲該項目創建自己的位圖並將其添加到WM中,以便可以在不受視圖邊界約束的情況下移動該位圖。當您收到ACTION_UP或等效事件時,將當前的x,y映射到拖動項目正下方的實際視圖(Rect類可能會有所幫助)。然後,您可以將該項目添加到該特定視圖。

+0

您好Jignesh,首先upvoted並感謝解釋這一點,似乎你已經與android啓動器,現在我正在研究「launcher3」的代碼源,我明白你的答案,但我找不到他們在哪裏(類)創建位圖並將其添加到WM(我正在尋找一個例子開始)。順便說一句ACTION_UP ...和Rect類非常有幫助。 – 2015-04-17 10:35:20