2016-12-20 3 views
0

Я использую UICollectionView для хранения изображений, и я могу их переупорядочить, переопределяя CanMove и MoveItem.Xamarin: UICollection Image reorder Issue

Но элементы внутри UICollection только изменяют размер ячейки, если размер ячейки составляет около 106 по высоте и ширине, тогда они могут быть переупорядочены, если они меньше по размеру, они не могут быть переупорядочены.

Вид:

 public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
//ImageCv is the name of UiCollectionView 
      var collectionLayout = new PostImageFlowLayout(3, 0.85f); 
      var allCollectionSource = new PostImageColectionSource(ImageCv, (ViewModel as NewPostDetailViewModel)); 
      ImageCv.RegisterNibForCell(PostImageCell.Nib, PostImageCell.Key); 
      ImageCv.RegisterClassForSupplementaryView(typeof(CollectionHeader), UICollectionElementKindSection.Header, new NSString("headerId")); 
      ImageCv.BackgroundColor = UIColor.Clear; 
      ImageCv.Hidden = false; 
      ImageCv.DataSource = allCollectionSource; 
      ImageCv.Delegate = collectionLayout; 

      var longPressGesture = new UILongPressGestureRecognizer(gesture => 
       { 
       // Take action based on state 
        switch (gesture.State) 
        { 
         case UIGestureRecognizerState.Began: 
          var selectedIndexPath = ImageCv.IndexPathForItemAtPoint(gesture.LocationInView(View)); 
          if (selectedIndexPath != null) 
           ImageCv.BeginInteractiveMovementForItem(selectedIndexPath); 

          Debug.WriteLine("Gesture Recognition: Activated"); 
          break; 
         case UIGestureRecognizerState.Changed: 
          ImageCv.UpdateInteractiveMovement(gesture.LocationInView(View)); 

          Debug.WriteLine("Gesture activated: Item location is changed"); 
          break; 
         case UIGestureRecognizerState.Ended: 
          ImageCv.EndInteractiveMovement(); 
          Debug.WriteLine("Gesture activation: complete"); 
          break; 
         default: 
          ImageCv.CancelInteractiveMovement(); 
          Debug.WriteLine("Gesture activation: Terminate"); 
          break; 
        } 
       }); 

      // Add the custom recognizer to the collection view 
      ImageCv.AddGestureRecognizer(longPressGesture); 
} 

UICollectionViewDelegateFlowLayout с использованием системы; с использованием System.Windows.Input; с использованием CoreGraphics; с использованием UIKit;

namespace Sources.CollectionSources 
{ 
    public class PostImageFlowLayout : UICollectionViewDelegateFlowLayout 
    { 
     private float headerHeight; 
     private int noOfItems; 


     private bool isLoading; 

     public PostImageFlowLayout(int noOfItems, float headerHeight = 0f) 
     { 
      this.noOfItems = noOfItems; 
      this.headerHeight = headerHeight; 
     } 
     public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath) 
     { 
      return GetPostCellSize(); 
     } 

     public override CGSize GetReferenceSizeForHeader(UICollectionView collectionView, UICollectionViewLayout layout, nint section) 
     { 
      return new CGSize(collectionView.Frame.Width, headerHeight); 
     } 

     public override UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section) 
     { 
      return new UIEdgeInsets(0, 0, 0, 0); 
     } 

     private CGSize GetPostCellSize() 
     { 
      var relativeWidth = (UIScreen.MainScreen.Bounds.Width - 2)/this.noOfItems; 
      return new CGSize(relativeWidth, relativeWidth); 

      //return new CGSize(55, 55); 
     } 

    } 
} 

Источник

public class PostImageColectionSource : MvxCollectionViewSource 
    { 
     private NewPostDetailViewModel newPostDetailViewModel; 
     private string type; 

     static NSString animalCellId = new NSString("PostImageCell"); 
     static NSString headerId = new NSString("Header"); 
     List<IAnimal> animals; 

     protected override NSString DefaultCellIdentifier 
     { 
      get 
      { 
       return PostImageCell.Key; 
      } 
     } 

     public override System.Collections.IEnumerable ItemsSource 
     { 
      get 
      { 
       return base.ItemsSource; 
      } 
      set 
      { 
       base.ItemsSource = value; 
       CollectionView.ReloadData(); 
      } 
     } 


     public PostImageColectionSource(UICollectionView collectionView, NewPostDetailViewModel newPostDetailViewModel) : base(collectionView) 
     { 
      this.newPostDetailViewModel = newPostDetailViewModel; 
      animals = new List<IAnimal>(); 
      for (int i = 0; i < 20; i++) 
      { 
       animals.Add(new Monkey(i)); 
      } 
     } 

     public override nint NumberOfSections(UICollectionView collectionView) 
     { 
      return 1; 
     } 

     public override nint GetItemsCount(UICollectionView collectionView, nint section) 
     { 
      return 5;// animals.Count; 
     } 

     public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) 
     { 
      var cell = (PostImageCell)collectionView.DequeueReusableCell(animalCellId, indexPath); 
      var animal = animals[indexPath.Row]; 
      cell.Result(indexPath.Row); 
      return cell; 
     } 

     public override bool CanMoveItem(UICollectionView collectionView, NSIndexPath indexPath) 
     { 
      Debug.WriteLine("Ready to move images"); 
      //System.Diagnostics.Debug.WriteLine("Checking if it can move the item"); 
      return true; 
     } 

     public override void MoveItem(UICollectionView collectionView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath) 
     { 
      //base.MoveItem(collectionView, sourceIndexPath, destinationIndexPath); 
      Debug.WriteLine("Start moving images to reorder"); 
      var item = animals[(int)sourceIndexPath.Item]; 
      animals.RemoveAt((int)sourceIndexPath.Item); 
      animals.Insert((int)destinationIndexPath.Item, item); 
     } 
    } 

Когда GetPostCellSize в PostImageFlowLayout имеет ширину и высоту около 100, в CanMove и MoveItem в PostImageColectionSource в настоящее время называется и детали быть заказана. Но если GetPostCellSize имеет ширину и высоту около 50 или 70, несмотря на то, что жесты активированы, CanMove и MoveItem в PostImageColectionSource не вызываются, поэтому их нельзя перемещать.

Можно ли надеяться, меня с переназначения изображения в UICollectionView, когда размер ячейки мал, как вокруг ширины и высоты 70.

Спасибо.

Я мечения быстрый и Objective-C, как эта проблема связана с IOS в целом, а не Xamarin конкретных

+0

Это действительно беспорядок, не может понять. Вы просто хотите заказать элементы в UICollectionView «Drag And Drop»? –

ответ

1

Основной проблемой здесь является то, что вам необходимо пройти в целях сбора на gesture.LocationInView (View) вместо основного вида. В ViewDidLoad в изменении UILongPressGestureRecognizer:

var selectedIndexPath = ImageCv.IndexPathForItemAtPoint(gesture.LocationInView(View)); 

и

ImageCv.UpdateInteractiveMovement(gesture.LocationInView(View)); 

к

var selectedIndexPath = ImageCv.IndexPathForItemAtPoint(gesture.LocationInView(ImageCv)); // <-- pass in ImageCV instead of View. (where ImageCV is the collection view) 

и

ImageCv.UpdateInteractiveMovement(gesture.LocationInView(ImageCv)); // <-- pass in ImageCV instead of View. 

Другое дело отметить, но не огромная сделка , состоит в том, что PostImageColectionSource в конечном счете получена из UICollectionViewSource, которая представляет собой комбо UICollectionViewDelegate и UICollectionViewDataSource в одном классе, но присваивается свойству коллекции DataSource. Все это означает, что, хотя вы можете реализовать методы для UICollectionViewDelegate в PostImageColectionSource, методы делегата не будут вызываться в этом классе, так как свойство Delegate вида коллекции установлено в PostImageFlowLayout, которое в конечном итоге получается от UICollectionViewDelegate через UICollectionViewDelegateFlowLayout.