Fix a crash caused by ViewHolder.getAdapterPosition returning -1

pull/1027/head
Alexander Bakker 3 years ago
parent e77df1eee4
commit 715c5112ab

@ -1,10 +1,13 @@
package com.beemdevelopment.aegis.helpers; package com.beemdevelopment.aegis.helpers;
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.beemdevelopment.aegis.vault.VaultEntry;
import com.beemdevelopment.aegis.ui.views.EntryAdapter; import com.beemdevelopment.aegis.ui.views.EntryAdapter;
import com.beemdevelopment.aegis.vault.VaultEntry;
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback { public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
@ -44,19 +47,23 @@ public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
} }
@Override @Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; // It's not clear when this can happen, but sometimes the ViewHolder
// that's passed to this function has a position of -1, leading
// to a crash down the line.
int position = viewHolder.getAdapterPosition();
if (position == NO_POSITION) {
return 0;
}
int swipeFlags = 0; int swipeFlags = 0;
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
if (viewHolder != null) { EntryAdapter adapter = (EntryAdapter) recyclerView.getAdapter();
int position = viewHolder.getAdapterPosition(); if (adapter.isPositionFooter(position)
EntryAdapter adapter = (EntryAdapter)recyclerView.getAdapter();
if (adapter.isPositionFooter(position)
|| adapter.getEntryAt(position) != _selectedEntry || adapter.getEntryAt(position) != _selectedEntry
|| !isLongPressDragEnabled()) || !isLongPressDragEnabled()) {
{ dragFlags = 0;
dragFlags = 0;
}
} }
return makeMovementFlags(dragFlags, swipeFlags); return makeMovementFlags(dragFlags, swipeFlags);

Loading…
Cancel
Save