简体   繁体   English

ScrollTo无法使用Xamarin.Forms中的分组ListView

[英]ScrollTo not working with grouped ListView in Xamarin.Forms

I have a TabbedPage and it contains a few ContentPage that are consisting of a ContentView and some of them has a ListView . 我有一个TabbedPage ,它包含一些由ContentView组成的ContentPage ,其中一些有一个ListView

The ListView contains a grouped ItemsSource represented by a custom model class: ListView包含由自定义模型类表示的分组 ItemsSource

public class DataGroup: List<DataGroupItem>
{   
    public string HeaderText { get; set; }

    public string ShortName { get; set; } 

    public string SubTitle { get; set; }

    public DataGroup(string headerText, string shortName)
    {
        HeaderText= headerText;
        ShortName= shortName;
    }
}

The XAML code (it has an ItemTemplate as well): XAML代码(它也有一个ItemTemplate):

<ListView x:Name="listView"
              Grid.Row="0"
              GroupDisplayBinding="{Binding HeaderText}"
              GroupShortNameBinding="{Binding ShortName}"
              HasUnevenRows="True"
              IsGroupingEnabled="True"
              ItemsSource="{Binding DataGroups}"
              SeparatorColor="Black"
              SeparatorVisibility="Default">
    ...
</ListView>

When I change the actual tab, I need the ListView to be scrolled to a certain group. 当我更改实际选项卡时, 我需要将ListView滚动到某个组。

I use this code: 我用这个代码:

ObservableCollection<DataGroup> dataGroups
    = this.listView.ItemsSource as ObservableCollection<DataGroup>;
if (scrolling)
{        
    var res = dataGroups.Where(dg=>dg.ItemCount > 3).LastOrDefault();
    if (res != null)
        //Device.StartTimer(TimeSpan.FromMilliseconds(150), () =>
        //{
            this.listView.ScrollTo(res, ScrollToPosition.Center, false);
            //return false;
        //});
}

I tried all variations in ScrollToPosition , unfortunately, nothing happens. 我尝试了ScrollToPosition所有变体,遗憾的是,没有任何反应。

What else should I try? 我还应该尝试什么?

Well, it was quite an adventure to figure this out. 好吧,想出这个就是一次冒险。

First, I thought I should set a child item as the target of the ScrollTo method, but it changed nothing. 首先,我认为我应该将子项设置为ScrollTo方法的目标,但它没有改变任何内容。

As a vague idea, I added the SelectedItem into the soup, and it worked, so I have this as a final result: 作为一个模糊的想法,我将SelectedItem添加到汤中,它有效,所以我将此作为最终结果:

ObservableCollection<DataGroup> dataGroups= this.listView.ItemsSource as ObservableCollection<DataGroup>;
if (scrolling)
{        
  var res = dataGroups.Where(dg=>dg.ItemCount > 3).LastOrDefault();
  if (res != null && res.Count > 0)
  {
    this.listView.SelectedItem = res[0];
    this.listView.ScrollTo(this.listView.SelectedItem, ScrollToPosition.MakeVisible, false);
  }
}

Try to use res[0] instead of res, something like this: 尝试使用res [0]而不是res,如下所示:

ObservableCollection<DataGroup> dataGroups= this.listView.ItemsSource as ObservableCollection<DataGroup>;
if (scrolling)
{        
  var res = dataGroups.Where(dg=>dg.ItemCount > 3).LastOrDefault();
  if (res != null && res.Count > 0)
  {
    this.listView.ScrollTo(res[0], ScrollToPosition.MakeVisible, false);
  }
}

Don't forget to set also the group. 别忘了也要小组。 You need to set both the group and the item, see example below: 您需要同时设置组和项目,请参阅下面的示例:

listView.ScrollTo(item, group, ScrollToPosition.Start, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM