2016-12-20 6 views
0

私はUICollectionViewを使って画像を保存しており、CanMoveMoveItemを上書きして並べ替えることができます。Xamarin:UICollection Image reorder問題

しかし、セルサイズが106の高さと幅のようにセルサイズが大きい場合は、UICollection内の項目の順序が変更されますが、サイズが小さい場合は並べ替えることができます。

ビュー:システムを使用して

 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を使用します。 PostImageFlowLayoutでGetPostCellSizeは、約100の幅と高さを持っている場合

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); 
     } 
    } 

は、PostImageColectionSourceCanMoveMoveItemが呼び出されているとの項目が並べ替えられています。しかし、GetPostCellSizeの幅と高さが約50または70の場合、ジェスチャが有効になっていてもCanMoveMoveItemPostImageColectionSourceには移動されないため、移動できません。

は、誰もが70

の幅と高さの周りにありがとうのようなセルサイズが小さい場合UICollectionViewで画像を並べ替えで私を望むことができます。

私はこの問題は、一般的にはIOSに関連しているとして、迅速かつ客観-Cタグ付け、ここで具体的な

+0

これは本当に混乱しており、その点を得ることはできません。 UICollectionViewのアイテムを「ドラッグ&ドロップ」で並べ替えるだけですか? –

答えて

1

主な問題をxamarinていないですが、あなたがgesture.LocationInView(ビュー)のコレクションビューに渡す必要があるということですメインビューの代わりに呼び出します。

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. 

注意すべきもうひとつはなく、大きな問題に

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

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

:UILongPressGestureRecognizerの変化ViewDidLoadPostImageColectionSourceは、UICollectionViewSourceから派生したものであり、1つのクラスでUICollectionViewDelegateUICollectionViewDataSourceのコンボであるが、コレクションビューのプロパティに割り当てられている。これは、UICollectionViewDelegateのメソッドを実装することはできますが、コレクションビューのDelegateプロパティはPostImageFlowLayoutに設定されているため、UICollectionViewDelegateFlowLayoutからUICollectionViewDelegateに派生しているため、そのクラスでデリゲートメソッドは呼び出されません。

関連する問題