0

我試圖把一些數字的水平列表爲ListView控件的一個項目,我做了,看起來不錯..應用TapGestureRecognizer在水平列表中的標籤 - Xamarin窗體

Sample screenshot of horizontal list as item in ListView

我想要接下來的是當我滾動任何項目的水平列表時,我希望自動選擇水平列表中的中間一個,並且我希望選定的值顯示在該項目的標籤中,因此我想要其他水平列表也是自動滾動並執行相同操作(各個標籤的所有值應分別保持其偏移值的差異)。

這是些棘手的問題,我知道GestureRecognizer在這裏會有所幫助,但我很困惑在這裏,以及如何在這裏實現它,因爲我是xamarin的新手.. 我將編寫所有將上述屏幕截圖作爲輸出的代碼在這裏..

namespace ViewsAndComponents 
{ 
    class LVItem : INotifyPropertyChanged 
    { 
    private double _offset; 
    private string _num; 

    public string Num 
    { 
     get { return _num; } 
     internal set 
     { 
      _num = value; 
      OnPropertyChanged("Num"); 
     } 
    } 

    public double Offset 
    { 
     get { return _offset; } 
     internal set 
     { 
      _offset = value; 
      OnPropertyChanged("Offset"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public partial class SVInsideLVItem : ContentPage 
{ 
    ObservableCollection<LVItem> Items = new ObservableCollection<LVItem>(); 
    ListView timePlannerLV; 

    Label tL; 

    public SVInsideLVItem() 
    { 
     InitializeComponent(); 

     Items.Add(new LVItem() { Num = "label-1", Offset = 5 }); 
     Items.Add(new LVItem() { Num = "label-2", Offset = 1 }); 
     Items.Add(new LVItem() { Num = "label-3", Offset = 3 }); 
     Items.Add(new LVItem() { Num = "label-4", Offset = 2 }); 
     Items.Add(new LVItem() { Num = "label-5", Offset = 4 }); 

     timePlannerLV = new ListView 
     { 
      // Source of data items. 
      ItemsSource = Items, 
      HasUnevenRows = true, 
      RowHeight = -1, 

      //each item; it must return a Cell derivative.) 
      ItemTemplate = new DataTemplate(() => 
       { 

        Label numL = new Label() 
        { 
         TextColor = Color.Black, 
         HorizontalTextAlignment = TextAlignment.Start, 
         FontSize = Device.GetNamedSize(NamedSize.Small, new Label()) 
        }; 

        numL.SetBinding<LVItems>(Label.TextProperty, indexer => indexer.Num); 


        List<int> items = new List<int>(); 
        items.Add(1); 
        items.Add(2); 
        items.Add(3); 
        items.Add(4); 
        items.Add(5); 
        items.Add(6); 
        items.Add(7); 
        items.Add(8); 
        items.Add(9); 
        items.Add(10); 
        items.Add(11); 
        items.Add(12); 
        items.Add(13); 
        items.Add(14); 
        items.Add(15); 
        items.Add(16); 
        items.Add(17); 
        items.Add(18); 
        items.Add(19); 
        items.Add(20); 

        StackLayout sLayout = new StackLayout() 
        { 
         Orientation = StackOrientation.Horizontal, 

        }; 

        for (int i = 0; i < items.Count; i++) 
        { 
         Label label = new Label() 
         { 
          HorizontalTextAlignment = TextAlignment.Center, 
          TextColor = Color.Black, 
          FontSize = Device.GetNamedSize(NamedSize.Small, new Label()) 
         }; 

         label.Text = items[i].ToString(); 

         sLayout.Children.Add(label); 
        } 

        ScrollView scroll = new ScrollView 
        { 
         Orientation = ScrollOrientation.Horizontal, 
         Content = new StackLayout 
         { 
          Children = 
         { 
          sLayout 
          } 

         } 
        }; 

        AbsoluteLayout layout = new AbsoluteLayout(); 
        AbsoluteLayout.SetLayoutFlags(numL, AbsoluteLayoutFlags.All); 
        AbsoluteLayout.SetLayoutBounds(numL, new Rectangle(0.2, 0.2, 0.8, 0.25)); 

        AbsoluteLayout.SetLayoutFlags(scroll, AbsoluteLayoutFlags.All); 
        AbsoluteLayout.SetLayoutBounds(scroll, new Rectangle(0.3, 0.6, 0.8, 0.2)); 


        layout.Children.Add(numL); 
        layout.Children.Add(scroll); 

        return new ViewCell 
        { 
         View = new StackLayout 
         { 
          Children = 
         { 
          layout, 
          new BoxView{HeightRequest=1,BackgroundColor=Color.Gray} 
         } 

         } 
        }; 
       }) 
     }; 

     this.Content = new StackLayout 
     { 
      Children = 
      { 
        timePlannerLV 
      } 
     }; 
    } 

} 

} 

任何幫助,將不勝感激。在此先感謝..

回答

0

我不能完全確定要在這裏做什麼。但你需要的gesturerecognizer添加到標籤你生成。所以,像這樣添加它:

Label label = new Label() 
{ 
    HorizontalTextAlignment = TextAlignment.Center, 
    TextColor = Color.Black, 
    FontSize = Device.GetNamedSize(NamedSize.Small, new Label()) 
}; 

label.Text = items[i].ToString(); 

var gestureRecognizer = new TapGestureRecognizer { 
    TappedCallback = o => selectedLabel.Text = o, 
    NumberOfTapsRequired = 1 
}; 

label.GestureRecognizers.Add (gestureRecognizer); 

sLayout.Children.Add(label); 

希望這可以幫助你相處。