在gridview中拖动项目

时间:2011-08-22 09:53:33

标签: android gridview drag-and-drop

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

1 个答案:

答案 0 :(得分:1)

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

  

拖动代码                让我们看看控件实现以及我们如何处理拖动项目。

        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
     

事件存储将项目拖入   DragEventArgs.Data.Properties [“Items”]的值。你会覆盖它   如果需要,可以设置自定义拖动数据的方法。        当用户拖动项目时,我们需要显示关于项目放置的位置的提示。标准的GridView处理这个问题   通过滑动相邻的物品。我们将完全实现这一点   我们在GridViewEx中行为,因为我们需要考虑案例   其中GridView不支持删除。

    /// <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
     

处理标准GridView执行的任何ItemsPanel的删除   不支持丢弃。

 /// <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
     

是最终用户操作的请求。

有关详细信息,请参阅以下链接Extending GridView with Drag and Drop for Grouping and Variable Sized Items

您也可以按照以下链接Android Drag and Drop Example

希望,这可能会对你有帮助。