2014-01-06 65 views
0

我已經動態創建了選項卡,然後在其中通過拖動控制器以編程方式添加GridView。如果它只有一個選項卡,它就可以工作,但如果您滾動到第二個選項卡,則重新排列項目然後返回,就會崩潰。我相信它是因爲兩個標籤都使用相同的mDragController。它應該是相當容易修復,任何想法如何讓他們使用單獨的控制器?在動態創建的GridViews中拖放

private DragController mDragController; 
private boolean mLongClickStartsDrag = true; // If true, it takes a long click 

protected void onCreate(Bundle savedInstanceState) { 
... 

    final TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost); 
    Tabs.setup(); 
    int count; 
     for (count =0;count < 2;count++){ 

      ... 

      final int passedTabId = count; 
      NewTab.setContent(new TabHost.TabContentFactory() 
      { 
       public View createTabContent(String tag) 
       { 

        ... 

        GridView dynGrid = new GridView(ManageRooms.this); 
        ... 
        mDragController = new DragController (ManageRooms.this); 
        dynGrid.setAdapter (new ImageCellAdapter (ManageRooms.this, mDragController)); 
        layout.addView(dynGrid); 


        return layout; 
       } 

      }); 

      Tabs.addTab(NewTab); 
     } 
} 

public boolean startDrag (View v) { 
    v.setOnDragListener (mDragController); 
    mDragController.startDrag (v); 
    return true; 
} 
} 

回答

0

解決方案:製作一個mDragControllers數組;

private DragController[] mDragController = new DragController[3]; 

然後根據當前標籤的ID訪問每一個。

+0

sir請回答這個問題[link](http://stackoverflow.com/questions/20988505/open-child-activity-in-tab-host-android) –