【问题标题】:Dragging items in gridview在gridview中拖动项目
【发布时间】:2011-08-22 09:53:33
【问题描述】:

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

【问题讨论】:

标签: android gridview drag-and-drop


【解决方案1】:

在这里我想再添加一个例子,参考和一些代码sn-p。

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

        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不支持drop。

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多