在gridview中拖动项目

Bud*_*ril 8 android gridview drag-and-drop

我有一个动态行数和3列的网格.在某个时刻只能看到3行.在网格中我可以有空单元格.您是否知道如何在单元格的视图上实现视图的拖放功能?我希望能够在空单元格中拖动项目.

小智 1

在这里我想再添加一个示例、参考和一些代码片段。

\n\n
\n

拖动代码\n 让\xe2\x80\x99s 看看控件的实现以及我们如何处理拖动项。

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

事件将拖动的项目存储到\n DragEventArgs.Data.Properties[\xe2\x80\x9cItems\xe2\x80\x9d] 值中。如果需要,您可以重写此\n 方法来设置自定义拖动数据。\n 当用户拖动某个项目时,我们需要显示有关该项目在放下后将放置在何处的提示。标准 GridView 通过将相邻项目滑开来处理此问题。我们将在 GridViewEx 中自己实现这一精确的行为,因为我们需要考虑 GridView 不支持拖放的情况。

\n\n
    /// <summary>\n    /// Shows reoder hints while custom dragging.\n    /// </summary>\n    protected override void OnDragOver(DragEventArgs e)\n    {\n        // see attached sample }\n\n    private int GetDragOverIndex(DragEventArgs e)\n    {\n        // see attached sample \n    }\n\n\n Dropping Code\n Next, let\xe2\x80\x99s look at the code that handles dropping.\n 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\n
Run Code Online (Sandbox Code Playgroud)\n\n

处理标准 GridView 不支持删除的任何 ItemsPanel 的删除。

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

由最终用户操作请求。

\n
\n\n

有关更多详细信息,您可以参考以下链接通过拖放扩展 GridView 以进行分组和可变大小的项目

\n\n

您也可以点击以下链接Android 拖放示例

\n\n

希望,这可以帮助你。

\n