Xaml
<ContentPage.Content>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<syncfusion:SfListView x:Name="listView" ItemSize="80" Grid.Row="0"
IsStickyGroupHeader="True" SelectionMode="None"
ItemsSource="{Binding contactsinfo}">
</syncfusion:SfListView>
<Grid Grid.Row="0" x:Name="LabelForGrid" HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand"/>
</Grid>
</ContentPage >
Behavior.cs
public class Behavior : Behavior<ContentPage>
{
private Grid grid;
private Label label;
private Label previousLabel;
public SfListView ListView { get; private set; }
protected override void OnAttachedTo(ContentPage bindable)
{
ListView = bindable.FindByName<SfListView>("listView");
grid = bindable.FindByName<Grid>("LabelForGrid");
ListView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
{
PropertyName = "ContactName",
KeySelector = (object obj1) =>
{
var item = (obj1 as Contacts);
return item.ContactName[0].ToString();
}
});
ListView.Loaded += ListView_Loaded;
base.OnAttachedTo(bindable);
}
private void ListView_Loaded(object sender, ListViewLoadedEventArgs e)
{
var groupcount = this.ListView.DataSource.Groups.Count;
for (int i = 0; i < groupcount; i++)
{
label = new Label(); //New labels created based on the group header key value
GroupResult group = ListView.DataSource.Groups[i];
label.Text = group.Key.ToString();
grid.Children.Add(label, 0, i);
var labelTapped = new TapGestureRecognizer() { Command = new Command<object>(OnTapped), CommandParameter = label };
label.GestureRecognizers.Add(labelTapped);
}
}
private void OnTapped(object obj)
{
if (previousLabel != null)
{
previousLabel.TextColor = Color.DimGray;
}
var label = obj as Label;
var text = label.Text;
label.TextColor = Color.Red;
for (int i = 0; i < ListView.DataSource.Groups.Count; i++)
{
var group = ListView.DataSource.Groups[i];
if (group.Key == null)
App.Current.MainPage.DisplayAlert("Message", "Group not available", "ok");
if ((group.Key != null && group.Key.Equals(text)))
{
ListView.LayoutManager.ScrollToRowIndex(ListView.DataSource.DisplayItems.IndexOf(group), true);
}
}
previousLabel = label;
}
}
|