Saving Re-Order Items In ListView Which Have Grouping Enable.

i want to update the listview collection which is implemented as Grouping , when i reorder item within the group , i am unable to update the OrderIndex . As I checked this Link but this is for single listview not grouped one : https://www.syncfusion.com/forums/136647/saving-re-ordered-items , Can You Help me Out from this.



Inside every group i have list of items which i am able to re-order and need to update the index position in my collection properties , This same can be happen in other group list also. Note that i am using single Listview with Grouping enable and also item drag and drop with limitation that item will not move in other group.

Please note that i have one Collection List From Item 1 to N in which i have done grouping , as i am checking that e.oldindx and e.newindex  i am getting sometime same and also it is not same as collection index , getting error in index out of range as i have also done below for the first time so i have one blank item always present which making index wrong.  i have done this in my code also so that group header will show without items m But as One items is always preset but that ID is Zero so we are removing the size of the using as present in this link : https://www.syncfusion.com/kb/8491/how-to-display-group-header-without-items-and-add-items-in-the-group-at-run-time . 


Please share some useful link i have checked


I have checked : https://help.syncfusion.com/xamarin/listview/item-drag-and-drop#reorder-the-underlying-collection but not helped in situation where Grouping is done in listview.


I need one example where Grouping is done in Listview with Drag and Drop feature enable and Updating the collection when drag and drop completed. Drag Item cannot be moved to Next Group also. Inside every Group drag and Drop need to work with showing there position in UI, which will update when user done dragging and dropping.


1 Reply 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team June 3, 2020 08:24 PM UTC

Hi Amit, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Saving reorder items with grouping in SfListView” from our end. We would like to inform you that the index in the ItemDraggingEventArgs will be counted including groups. So, we suggest you to consider the group index when updating the item index. Please refer the following code snippets for more reference, 
 
public class ListViewBehavior : Behavior<ContentPage> 
{ 
SfListView ListView; 
VisualContainer container; 
ViewModel ViewModel; 
 
protected override void OnAttachedTo(ContentPage bindable) 
{ 
    ListView = bindable.FindByName<SfListView>("listView"); 
    ViewModel = new ViewModel(); 
    ListView.BindingContext = ViewModel; 
    ListView.ItemDragging += ListView_ItemDragging; 
    container = this.ListView.GetVisualContainer(); 
 
    base.OnAttachedTo(bindable); 
} 
 
private async void ListView_ItemDragging(object sender, ItemDraggingEventArgs e) 
{ 
    var currentGroup = this.GetGroup(e.ItemData); 
 
    //To skip dragging into another group. 
    if (e.Action == DragAction.Dragging) 
    { 
        var groupIndex = this.ListView.DataSource.Groups.IndexOf(currentGroup); 
        var nextGroup = (groupIndex + 1 < this.ListView.DataSource.Groups.Count) ? this.ListView.DataSource.Groups[groupIndex + 1] : null; 
        ListViewItem groupItem = null; 
        ListViewItem nextGroupItem = null; 
 
        foreach (ListViewItem item in container.Children) 
        { 
            if (item.BindingContext == null || !item.Visibility) 
                continue; 
 
            if (item.BindingContext.Equals(currentGroup))    
                groupItem = item; 
 
            if (nextGroup != null && item.BindingContext.Equals(nextGroup)) 
                nextGroupItem = item; 
        } 
 
        if (groupItem != null && e.Bounds.Y <= groupItem.Y + groupItem.Height || nextGroupItem != null && (e.Bounds.Y + e.Bounds.Height >= nextGroupItem.Y)) 
            e.Handled = true; 
    } 
             
    //To update the index after reordering. 
    if(e.Action == DragAction.Drop) 
    { 
        await Task.Delay(100); 
 
        //To get the group index of the current item. 
        var groupIndex = this.ListView.DataSource.Groups.IndexOf(currentGroup) + 1; 
        UpdateIndex(e, groupIndex); 
    } 
} 
 
private void UpdateIndex(object eventArgs, int groupIndex) 
{ 
    var args = eventArgs as ItemDraggingEventArgs; 
 
    Device.BeginInvokeOnMainThread(() => 
    { 
        //Need to consider group index, since, index in the EventArgs will be counted including groups. 
        ViewModel.ToDoList.Move(args.OldIndex - groupIndex, args.NewIndex - groupIndex); 
 
        for (int i = 0; i < ViewModel.ToDoList.Count; i++) 
        { 
            (ViewModel.ToDoList[i] as ToDoItem).ID = i; 
        } 
    }); 
} 
} 
 
We have prepared the sample based on your requirement and attached in the following link, 
 
You can also refer to our user guidance document regarding grouping using the following link, 
 
Please check the sample and let us know if this meets your requirement. 
 
Regards, 
Lakshmi Natarajan 
 


Marked as answer
Loader.
Up arrow icon