a drag-and-drop scrolling grid view for Android
To start using DraggableGridView:
-
Place libs/DraggableGridView.jar into your project’s directory and add it to the build path (www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29)
-
Add an import to the activity you’d like to use it in: import com.animoto.android.views.*;
-
Add the view to your activity’s layout XML and save it:
<com.animoto.android.views.DraggableGridView android:id="@+id/dgv" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"> </com.animoto.android.views.DraggableGridView>
-
Obtain a reference to the view to use in code:
public class DraggableGridViewSampleActivity extends Activity { DraggableGridView dgv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dgv = ((DraggableGridView)findViewById(R.id.dgv)); }
-
You’re done! You can start adding any type of view you’d like.
TextView tv = new TextView(this); tv.setText("Hello world!"); dgv.addView(tv);
-
Tap and hold a view to begin moving it! Drag it between other views to rearrange them, and drop them in place.
-
Scales nicely to any screen size or resolution
-
Animates smoothly to indicate the rearrangement of children
-
Performs well, even with hundreds of children
-
Becomes scrollable when it contains more children than are visible
-
Can scroll even when a child is being dragged: just bring it to the very top or bottom
-
Easy to modify or extend!
The included project is an Android version of magnetic poetry, which you may have seen covering an eccentric friend’s refrigerator. It creates an OnRearrangeListener for its DraggableGridView which manipulates an ArrayList of strings.
If you’d rather not have to separately keep track of the grid’s elements, I suggest creating your own type of View that contains any information you need about the children and collecting this information from the DraggableGridView when you need it:
String[] words = new String[dgv.getChildCount()]; for (int i = 0; i < words.length; i++) words[i] = ((CustomView)dgv.getChildAt(i)).word;
-
Remove scrolling timer
-
Improve performance
-
This was developed targeting Android 2.2 (API level 8), though I’m sure it can be made to work on lower versions.
-
The sample project’s main activity doesn’t handle onConfigurationChanged, so it will restart if the screen orientation changes. I’ll fix this soon, but it’s not particularly important.
-
DraggableGridView and all associated code are © 2011, Animoto Inc, available under the Apache License, Version 2.0.
-
Check us out at www.animoto.com !