Changing the position of an item in UIListView in the chain table
Let's take a look at the mechanism of adding, inserting, and deleting UIListView, and then consider how to move a item in the linked list.
UIListView. h
/* Use an array to save all items */CCArray * _ items;
Void ListView: pushBackCustomItem (Widget * item) {/* add an item to the array */_ items-> addObject (item); remedyLayoutParameter (item); addChild (item ); _ refreshViewDirty = true;} void ListView: insertCustomItem (Widget * item, int index) {/* Insert an item to the array */_ items-> insertObject (item, index ); remedyLayoutParameter (item); addChild (item); _ refreshViewDirty = true;} void ListView: removeItem (int index) {Widget * item = getItem (I Ndex); if (! Item) {return;}/* remove an item from the array */_ items-> removeObject (item); removeChild (item); _ refreshViewDirty = true ;}
It can be seen that the management of all items in the linked list is only stored by arrays, so moving the position of an item is easy:
/* Declare a position movement function to move an item to a position in the linked list. Parameter 1: the current position of a item. Parameter 2: target Position */void changeItemOrder (int curIdx, int tarIdx) of an item );
Void ListView: changeItemOrder (int curIdx, int tarIdx) {if (curIdx = tarIdx) {return;} Widget * item = getItem (curIdx); if (! Item) {return;}/* re-adjust the position of the item: Remove and re-insert */_ items-> removeObject (item); _ items-> insertObject (item, tarIdx);/* adjust the layout and turn on the refresh switch */remedyLayoutParameter (item); _ refreshViewDirty = true; // sortAllChildren ();}
Usage:
ListView-> changeItemOrder (5, 0); // move the item with the index of 5 to the beginning of the linked list to become the first item