【问题标题】:Xamarin Forms ListView inside ListView ProblemXamarin Forms ListView里面的ListView问题
【发布时间】:2019-08-10 22:03:59
【问题描述】:
<ListView HasUnevenRows="True" x:Name="collectionView" HeightRequest="1000"
                  Margin="20">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout HeightRequest="50" Margin="10" WidthRequest="200">
                            <Label Text="{Binding Name}" FontSize="15" TextColor="Black"/>
                            <Label Text="{Binding Description}" FontSize="15" TextColor="Black"/>
                            <ListView HasUnevenRows="True" ItemsSource="{Binding appointments}" HeightRequest="1000" Margin="20,0,0,0">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout HeightRequest="100" Margin="10" WidthRequest="200">
                                                <Label Text="{Binding Name}" FontSize="15" TextColor="Black"/>
                                                <Label Text="{Binding Subject}" FontSize="15" TextColor="Black"/>
                                            </StackLayout>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>  



 protected override void OnAppearing()
        {
            try
            {
                base.OnAppearing();
                var appointemntsList = new List<Appointment>()
                {
                    new Appointment() { Name="Mohamed",Subject="Sub"},
                    new Appointment() { Name="Mohamed",Subject="Sub"},
                    new Appointment() { Name="Mohamed",Subject="Sub"},
                    new Appointment() { Name="Mohamed",Subject="Sub"},
                };
                var Monkeys = new List<Appointment>()
                {
                    new Appointment() { Name="Ahmed", Description="Desc",appointments=appointemntsList},
                    new Appointment() { Name="Ahmed", Description="Desc",appointments=appointemntsList},
                    new Appointment() { Name="Ahmed", Description="Desc",appointments=appointemntsList},
                    new Appointment() { Name="Ahmed", Description="Desc",appointments=appointemntsList},

                };
                collectionView.ItemsSource = Monkeys;
            }
            catch (Exception)
            {
            }
        }  

使用此代码我需要在列表视图中显示列表视图但不起作用

【问题讨论】:

  • 什么不起作用?
  • 在另一个可滚动控件中使用可滚动控件是一种不好的做法
  • 您在寻找某种分组方式吗?
  • @DennisSchröer 在 ListView 中显示 ListView 第一个 ListView 显示良好,但第二个未显示
  • @G.hakim 如果我想在 ListView 中显示列表视图该怎么办

标签: xamarin xamarin.forms


【解决方案1】:

你可以做的是用自定义的RepeaterView 替换内部的Xamarin.Forms.ListView

RepeaterView 是一个继承自堆栈布局的控件,其工作方式与 ListView 非常相似,但没有自己的滚动功能,因为您已经拥有一个 ListView,即“滚动”,它应该非常适合您。

public class RepeaterView<T> : StackLayout where T : class
{
    public static readonly BindableProperty HeaderTemplateProperty = BindableProperty.Create(nameof(HeaderTemplate), typeof(DataTemplate), typeof(RepeaterView<T>), default(DataTemplate));

    public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(RepeaterView<T>), default(DataTemplate));

    public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable<T>), typeof(RepeaterView<T>), null, defaultBindingMode: BindingMode.OneWay, propertyChanged: ItemsChanged);

    public RepeaterView()
    {
        Spacing = 0;
    }

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

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

    public DataTemplate HeaderTemplate
    {
        get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
        set { SetValue(HeaderTemplateProperty, value); }
    }

    protected virtual View ViewFor(T item)
    {
        View view = null;
        if (ItemTemplate != null)
        {
            var content = ItemTemplate.CreateContent();
            view = (content is View) ? content as View : ((ViewCell)content).View;

            view.BindingContext = item;
        }

        return view;
    }

    protected View HeaderView()
    {
        View view = null;

        if (HeaderTemplate != null)
        {
            var content = HeaderTemplate.CreateContent();
            view = (content is View) ? content as View : ((ViewCell)content).View;
            view.BindingContext = this.BindingContext;
        }

        return view;
    }

    private static void ItemsChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as RepeaterView<T>;
        if (control == null)
            return;

        control.Children.Clear();

        IEnumerable<T> items = (IEnumerable<T>)newValue;
        if (items.Any())
        {
            var header = control.HeaderView();
            if (header != null)
                control.Children.Add(header);

            foreach (var item in items)
                control.Children.Add(control.ViewFor(item));
        }
    }
}

为了更好地了解此控件的工作原理,您可以查看指南here

如有疑问,请随时回复

【讨论】:

    【解决方案2】:

    您需要为ListView 编写自定义渲染器,以便为Android 的ListView 启用嵌套滚动:

    public class NestedListViewRenderer: ListViewRenderer {
     Context _context;
    
     public NestedListViewRenderer(Context context): base(context) {
      _context = context;
     }
    
     protected override void OnElementChanged(ElementChangedEventArgs < Xamarin.Forms.ListView > e) {
      base.OnElementChanged(e);
    
      if (e.NewElement != null) {
       var listView = this.Control as Android.Widget.ListView;
       listView.NestedScrollingEnabled = true;
      }
     }
    }
    

    注意:不建议这样做,因为它可能会影响LisView 的性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 2018-01-17
      • 2017-01-05
      • 1970-01-01
      • 2020-04-09
      • 2019-09-15
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多