Click here to Skip to main content
15,887,967 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
INTRODUCTON:

I am working on a drag and drop feature of the listview. I am stuck at the last step -> rearranging multiple selected items after user finishes drag and drop.

QUESTION:

Can you explain me the algorithm that implements item rearrangment ( I wish to implement default drag & drop behavior ) ?

I have the index of the clicked item after user releases the mouse. I have indexes of dragged items and their count. Pseudo code is acceptable as well, but be warned that I might have follow up questions.

Regards.
Posted
Comments
Maciej Los 18-Feb-15 3:08am    
What have you tried till now? Where are you stuck?

1 solution

There is no function to move items inside the list. So the move process has to splitted into delete and insert operations.

For multiple items this can be done in two ways: Deleting all selected items first before inserting them at the new position or perform the delete / insert for each item. An example for single items can be found in the article Manual reordering of items inside a ListView[^]. The first method requires that all item data are stored in some way (usually in the clipboard when starting the drag operation).

Both methods require adjusting the indices of the source and destination position after each delete and insert operation.

Pseudo code for the second method:

  • Iterate through the selected items
  • Copy item data
  • Delete item
  • Decrement insert index if greater than index of deleted item
  • Decrement those remaining source indices that are greater than index of deleted item (this will be all when the iteration is performed from smallest to largest index)
  • Insert item
  • Increment those remaining source indices that are greater or equal than the insert index
  • Increment insert index
 
Share this answer
 
Comments
AlwaysLearningNewStuff 28-Feb-15 15:15pm    
Thank you. Voted 5. Best regards until next time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900