DraggableGridView

Introduction: Item 可拖动交换位置的 GridView,实际是自己继承 ViewGroup 实现,类似桌面的单屏效果,可屏幕自动上下滚动进行 Item 移动交换,多屏效果见下面 PagedDragDropGrid
Other: 也可自定义item的宽高和每行的个数,同时修改了交换逻辑,当移动到另一个item时就进行交换,并删除滚动逻辑。
More: Author   ReportBugs   DemoAPK   
Tags:

= DraggableGridView

a drag-and-drop scrolling grid view for Android == Including in your project

To start using DraggableGridView:

  • Place libs/DraggableGridView.jar into your project's directory and add it to the build path (http://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:

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

== Features

  • 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 Sample Project

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;

== To Do

  • Remove scrolling timer
  • Improve performance

== Notes

  • 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 (c) 2011, Animoto Inc, available under the Apache License, Version 2.0.
  • Check us out at http://www.animoto.com !
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools