2017-10-06 14 views
0

私のアプリに横項目をリストしたいと思います。 ショーケースxamarinに印刷中のアプリが表示されていますが、同じケースです。xamarin形式の横のリストビュー項目

私はCarouselViewを使用しますが、フルセンスを表示します。

私に解決策を教えてください。おかげさまで あなたはリストビューを使用して、270度に回転を設定することができます

enter image description here

+1

お試しください。http://www.fabiocozzolino.eu/a-little-and-simple-bindable-horizo​​ntal-scroll-view/ –

答えて

0

Thisです。

あなたはあなたの要件の高さと幅を変更する必要がありますし、

Thisはあなたのための役に立つかもしれないことに注意してください。

2

ScrollViewとStackLayoutを使用して独自の水平リストを作成できます。

このような何か:

public class HorizontalList : Grid 
{ 
    private ICommand innerSelectedCommand; 
    private ScrollView scrollview; 
    private StackLayout mainPanel; 
    public event EventHandler SelectedItemChanged; 
    public static readonly BindableProperty SelectedCommandProperty = 
     BindableProperty.Create("SelectedCommand", typeof(ICommand), typeof(HorizontalList), null); 

    public static readonly BindableProperty ItemsSourceProperty = 
     BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HorizontalList), default(IEnumerable<object>), BindingMode.TwoWay, propertyChanged: ItemsSourceChanged); 

    public static readonly BindableProperty SelectedItemProperty = 
     BindableProperty.Create("SelectedItem", typeof(object), typeof(HorizontalList), null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged); 

    public static readonly BindableProperty ItemTemplateProperty = 
     BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(HorizontalList), default(DataTemplate)); 

    public ICommand SelectedCommand 
    { 
     get { return (ICommand)GetValue(SelectedCommandProperty); } 
     set { SetValue(SelectedCommandProperty, value); } 
    } 

    public IEnumerable ItemsSource 
    { 
     get { return (IEnumerable)GetValue(ItemsSourceProperty); } 
     set { SetValue(ItemsSourceProperty, value); } 
    } 

    public object SelectedItem 
    { 
     get { return (object)GetValue(SelectedItemProperty); } 
     set { SetValue(SelectedItemProperty, value); } 
    } 

    public DataTemplate ItemTemplate 
    { 
     get { return (DataTemplate)GetValue(ItemTemplateProperty); } 
     set { SetValue(ItemTemplateProperty, value); } 
    } 

    private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue) 
    { 
     var itemsLayout = (HorizontalList)bindable; 
     itemsLayout.SetItems(); 
    } 

    public HorizontalList() 
    { 
     scrollview = new ScrollView(); 
     mainPanel = new StackLayout 
     { 
      Orientation = StackOrientation.Horizontal; 
      HorizontalOptions = LayoutOptions.FillAndExpand 
     }; 
     scrollview.Content = mainPanel; 
     scrollview.Orientation = ScrollOrientation.Horizontal 
     Children.Add(scrollview); 
    } 

    protected virtual void SetItems() 
    { 
     mainPanel.Children.Clear(); 

     if (ItemsSource == null) 
     { 
      return; 
     } 

     innerSelectedCommand = new Command<View>(view => 
     { 
      SelectedItem = view.BindingContext; 
      SelectedItem = null; // Allowing item second time selection 
     }); 

     foreach (var item in ItemsSource) 
     { 
      mainPanel.Children.Add(GetItemView(item)); 
     } 

     SelectedItem = null; 
    } 

    protected virtual View GetItemView(object item) 
    { 
     var content = ItemTemplate.CreateContent(); 
     var view = content as View; 

     if (view == null) 
     { 
      return null; 
     } 

     view.BindingContext = item; 

     var gesture = new TapGestureRecognizer 
     { 
      Command = innerSelectedCommand, 
      CommandParameter = view 
     }; 

     AddGesture(view, gesture); 

     return view; 
    } 

    private void AddGesture(View view, TapGestureRecognizer gesture) 
    { 
     view.GestureRecognizers.Add(gesture); 

     var layout = view as Layout<View>; 

     if (layout == null) 
     { 
      return; 
     } 

     foreach (var child in layout.Children) 
     { 
      AddGesture(child, gesture); 
     } 
    } 

    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue) 
    { 
     var itemsView = (HorizontalList)bindable; 
     if (newValue == oldValue && newValue != null) 
     { 
      return; 
     } 

     itemsView.SelectedItemChanged?.Invoke(itemsView, EventArgs.Empty); 

     if (itemsView.SelectedCommand?.CanExecute(newValue) ?? false) 
     { 
      itemsView.SelectedCommand?.Execute(newValue); 
     } 
    } 
} 

Hereは、同様のアプローチの一例です。

+0

ありがとう、素晴らしい例です。それは私が多くを学ぶのを助けた。 –

+0

このリンクは質問に答えるかもしれませんが、答えの本質的な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューの投稿](レビュー/低品質の投稿/ 17545919) – khellang

+0

@khellang、指摘のおかげで。私は理解を深めるためにコードを追加しました。しかし、私はその問題を解決する方法を述べたと思います。 –

関連する問題