Dainius Vaičiulis Dainius Vaičiulis - 5 months ago 28
Android Question

Drag and Drop in dynamically created GridViews

I have dynamically created tabs, in which I then programatically add a GridView with drag

drop controller. It all works if its only one tab, but if you scroll to the second one, rearrange items and then back, it crashes. I believe its because both tabs are using the same mDragController. It should be fairly easy to fix, any Ideas how to make them use separate controllers?

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

return layout;



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


Solution: making an array of mDragControllers;

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

and then accessing each one according to the id of the current Tab.