Skip to content

Android app crashes when dragging into CollectionView #17823

@vitalii-vov

Description

@vitalii-vov

Description

If you add a Header to the CollectionView, then when you drag an element to the end of the list, a crash will occur.

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'index')'

Steps to Reproduce

  1. Create CollectionView with Header
  2. Drag item to the end of the collection

Link to public reproduction project repository

No response

Version with bug

7.0.92

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

You need to make a change to the code of the class Microsoft.Maui.Controls.Handlers.Items.ReorderableItemsViewAdapter

private readonly bool _hasHeader;   //  <- add this

public ReorderableItemsViewAdapter(TItemsView reorderableItemsView,
	Func<View, Context, ItemContentView> createView = null) : base(reorderableItemsView, createView)
{
	_hasHeader = reorderableItemsView.Header is not null;  //  <- add this
}

Change it

else if (itemsView.ItemsSource is IList list)
{
	var fromItem = list[fromPosition];
	SetObserveChanges(itemsSource, false);
	list.RemoveAt(fromPosition);
	list.Insert(toPosition, fromItem);
	NotifyItemMoved(fromPosition, toPosition);
	SetObserveChanges(itemsSource, true);
	itemsView.SendReorderCompleted();
	return true;
}

to something like that

else if (itemsView.ItemsSource is IList list)
{
	var sourceFromPosition = _hasHeader ? fromPosition - 1 : fromPosition;
	var sourceToPosition = _hasHeader ? toPosition - 1 : toPosition;

	var fromItem = list[sourceFromPosition];
	SetObserveChanges(itemsSource, false);
	list.RemoveAt(sourceFromPosition);
	list.Insert(sourceToPosition, fromItem);
	NotifyItemMoved(fromPosition, toPosition);
	SetObserveChanges(itemsSource, true);
	itemsView.SendReorderCompleted();
	return true;
}

Relevant log output

>	0x1ED in Microsoft.Maui.Controls.Handlers.Items.ReorderableItemsViewAdapter<Microsoft.Maui.Controls.ReorderableItemsView,Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource>.OnItemMove at D:\Downloads\maui-main\src\Controls\src\Core\Handlers\Items\Android\Adapters\ReorderableItemsViewAdapter.cs:69,5	C#
 	0x2D in Microsoft.Maui.Controls.Handlers.Items.SimpleItemTouchHelperCallback.OnMove at D:\Downloads\maui-main\src\Controls\src\Core\Handlers\Items\Android\SimpleItemTouchHelperCallback.cs:36,4	C#

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions