是否可以在Sectioned RecyclerView中使用拖放功能?
我在RecyclerView中找到了用于拖放的github链接。但我希望它能在分段回收视图中实现它。
请建议
void OnDragInsideRecyclerView(object sender, DragEventArgs e)
{
var theEvent = e.Event;
var view = (View)theEvent.LocalState;
var containerView = (RecyclerView)sender;
_currentContactPosition = containerView.GetChildAdapterPosition(view);
// Ensure the position is valid.
if (_currentContactPosition != -1)
{
_contactToMove = _adapter.MainViewModel.Contacts[_currentContactPosition];
}
switch (theEvent.Action)
{
case DragAction.Location:
View onTopOf = containerView.FindChildViewUnder(theEvent.GetX(), theEvent.GetY());
_newContactPosition = containerView.GetChildAdapterPosition(onTopOf);
// Ensure the new position is valid.
if (_newContactPosition != -1)
{
var layoutManager = (LinearLayoutManager)_recyclerView.GetLayoutManager();
int firstVisiblePosition = layoutManager.FindFirstCompletelyVisibleItemPosition();
int lastVisiblePosition = layoutManager.FindLastVisibleItemPosition();
// Scroll up or down one if we are over the first or last visible list item.
if (_newContactPosition >= lastVisiblePosition)
layoutManager.ScrollToPositionWithOffset(firstVisiblePosition + 1, 0);
else if (_newContactPosition <= firstVisiblePosition)
layoutManager.ScrollToPositionWithOffset(firstVisiblePosition - 1, 0);
// Update the location of the Contact
_adapter.MainViewModel.Contacts.Remove(_contactToMove);
_adapter.MainViewModel.Contacts.Insert(_newContactPosition, _contactToMove);
_adapter.NotifyItemMoved(_currentContactPosition, _newContactPosition);
}
e.Handled = true;
break;
case DragAction.Ended:
// Reset the visibility for the Contact item's view.
// This is done to reset the state in instances where the drag action didn't do anything.
view.Visibility = ViewStates.Visible;
// Boundary condition, scroll to top is moving list item to position 0.
if (_newContactPosition != -1)
{
_recyclerView.ScrollToPosition(_newContactPosition);
_newContactPosition = -1;
}
else
{
_recyclerView.ScrollToPosition(0);
}
e.Handled = true;
break;
}
}