Dragging items in a gridview

I have a grid with a dynamic number of rows and 3 columns. At a certain moment, only 3 lines are visible. In the grid, I can have empty cells. Do you know how to implement the drag and drop function for presenting in a view from a cell? I want to be able to drag items into empty cells.

+8
android gridview drag-and-drop
source share
1 answer

Here I want to add another example, a link and a code snippet.

Drag and Drop Code Let's look at the implementation of the control and how we handle the drag and drop of items.

public class GridViewEx : GridView { /// <summary> /// Initializes a new instance of the <see cref="GridViewEx"/> control. /// </summary> public GridViewEx() { // see attached sample } private void GridViewEx_DragItemsStarting(object sender, DragItemsStartingEventArgs e) { // see attached sample } /// <summary> /// Stores dragged items into DragEventArgs.Data.Properties["Items"] value. /// Override this method to set custom drag data if you need to. /// </summary> protected virtual void OnDragStarting(DragItemsStartingEventArgs e) { // see attached sample } The control has several fields which store the indices of several active items during the drag/drop process. The OnDragStarting 

event stores drag items to DragEventArgs.Data.Properties ["Items"] value. You would cancel this method for setting custom drag and drop data if you need to. When the user drags an item, we need to show hints about where the item will be placed if it is deleted. The standard GridView handles this by moving adjacent objects to the side. We will implement this precise behavior in a GridViewEx, because we need to consider cases where the GridView does not support deletion.

  /// <summary> /// Shows reoder hints while custom dragging. /// </summary> protected override void OnDragOver(DragEventArgs e) { // see attached sample } private int GetDragOverIndex(DragEventArgs e) { // see attached sample } Dropping Code Next, let's look at the code that handles dropping. We have to override GridView.OnDrop method which is called every time when an end-user drops an item to the new location. Our override 

handles dropping for any ItemsPanel that a standard GridView does not support resetting.

  /// <summary> /// Handles drag and drop for cases when it is not supported by the Windows.UI.Xaml.Controls.GridView control /// </summary> protected override async void OnDrop(DragEventArgs e) { // see attached sample } The OnDrop method includes logic for moving items from one group to another when grouping is enabled, and for new group creation if it 

requested by end user actions.

For more information, you can refer to the following links Drag and Drop GridView Extension for Grouping and Resizable Elements

You can also follow the link below Android Drag and Drop Example

Hope this can help you.

+1
source share

All Articles