Android open-source -- DragSortListview
Introduction:DragSortListview is a list that supports data deletion by dragging left and right, and sorting by dragging up and down. Its disadvantage is that
It is difficult to pull down when there are too many items.
API introduction:
DragSortListView. DropListener> this interface listens to listeners with changed positions when they are dragged up or down;
DragSortListView. RemoveListener> this interface is used to monitor the interfaces that successfully slide data when dragging left or right;
FloatViewManager: The Floating Box control that appears when you drag up or down includes the control of declaration and destruction.
DragSortListView: Set the drag switch, and set the callback listener, because the system does not know what we operate on
Specific business
DragSortController: This controller implements FloatViewManager and OnTouchListener;
Note: most operations generated by dragging are in the Controller. The delete operation switch is also Controller;
At the same time, the delete and drag modes are also in the Controller. There is a problem here. What we operate on
The system provides the setDragHandleId () method to set the operation View;
/*** A simple example * @ author Lean @ date: 2014-10-23 */public class MainActivity extends Activity {private DragSortListView mDslv; private DragSortController mController; ArrayAdapter
Adapter; private DragSortListView. DropListener onDrop = new DragSortListView. DropListener () {@ Override public void drop (int from, int to) {if (from! = To) {// call String item = adapter instantly when the finger is released when it is dragged up or down. getItem (from); adapter. remove (item); adapter. insert (item, to) ;}}; private DragSortListView. removeListener onRemove = new DragSortListView. removeListener () {@ Override public void remove (int which) {// The adapter is successfully called to delete the location. remove (adapter. getItem (which) ;}}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mDslv = (DragSortListView) findViewById (android. r. id. list); // open the drag switch. If the right-Sliding operation is not enabled, mDslv is not returned. setDragEnabled (true); // sets the listener mDslv. setDropListener (onDrop); mDslv. setRemoveListener (onRemove); mController = buildController (mDslv); // set the floating box manager and click the mDslv listener. setFloatViewManager (mController); mDslv. setOnTouchListener (mController); setListAdapter ();} public DragSortController buildController (DragSortListView dslv) {DragSortController controller = new DragSortController (dslv); // You can map the id of a drag object to View controller. setDragHandleId (R. id. drag_rl); // sets the controller for removing the switch. setRemoveEnabled (true); // sets the drag/remove Mode controller. setDragInitMode (DragSortController. ON_DRAG); controller. setRemoveMode (DragSortController. FLING_REMOVE); return controller;} public void setListAdapter () {String [] array = getResources (). getStringArray (R. array. jazz_artist_names); ArrayList
List = new ArrayList
(Arrays. asList (array); adapter = new ArrayAdapter
(This, R. layout. list_item_handle_left, R. id. text, list); mDslv. setAdapter (adapter );}}