2011-08-22 30 views
8

Tôi có lưới có số hàng động và 3 cột động. Chỉ có 3 hàng được hiển thị tại một thời điểm nhất định. Trong lưới tôi có thể có các ô trống. Bạn có biết cách triển khai tính năng kéo/thả cho chế độ xem trên chế độ xem từ ô không? Tôi muốn có thể kéo các mục trong các ô trống.Kéo các mục trong chế độ xem lưới

+0

http://developer.android.com/guide/topics/ui/drag-drop.html –

+0

Tôi cần thực hiện việc này trong android 2.1 –

+0

u muốn kéo và thả các mục trong ô trống – Jeetendra

Trả lời

1

Ở đây tôi muốn thêm một ví dụ nữa, tham chiếu và một số đoạn mã.

Mã kéo Hãy xem xét việc thực hiện kiểm soát và cách chúng tôi xử lý các mục kéo.

 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 

các cửa hàng sự kiện đã kéo các mục vào Giá trị DragEventArgs.Data.Properties ["Items"]. Bạn sẽ ghi đè phương thức này để đặt dữ liệu kéo tùy chỉnh nếu cần. Khi người dùng kéo một mục, chúng tôi cần hiển thị gợi ý về vị trí mục sẽ được đặt nếu bị bỏ. GridView chuẩn xử lý này bằng cách trượt các mục lân cận ra khỏi đường. Chúng tôi sẽ thực hiện chính xác hành vi này trong GridViewEx vì chúng tôi cần tính đến các trường hợp nơi GridView không hỗ trợ giảm.

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

xử lý giảm cho bất kỳ mục nàoPanel mà GridView tiêu chuẩn không không hỗ trợ giảm.

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

được yêu cầu bởi hành động của người dùng cuối.

Để biết thêm chi tiết bạn có thể tham khảo link sau Extending GridView with Drag and Drop for Grouping and Variable Sized Items

Bạn có thể làm theo các liên kết dưới đây quá Android Drag and Drop Example

Hope, điều này có thể giúp bạn.

Các vấn đề liên quan