Fixed IndexOutOfBoundsException when swiping penultimate+last item of th... #8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
...e list
Hi Roman,
First of all, thanks for your library.
I've discovered a bug in the library that is quite easy to reproduce: if you swipe the penultimate item of the list and just right after you start swiping the last element of the list (keep it pressed) before the performDismiss() method of the penultimate item gets called, the application throws an IndexOutOfBoundsException.
If I am not wrong, that happens because when you swipe the penultimate item, mAdapter.notifyDataSetChanged() gets called in the onDismiss() callback, the view gets refreshed, and MotionEvent.ACTION_UP gets called using the mDownPosition saved when MotionEvent.ACTION_DOWN. If performDismiss() gets called (depending on the X of the touch event), you might end up trying to remove an element with a stale position from the list. In case it is the last item of the list, that will lead to an IndexOutOfBoundsException.
I know the bug depends on the current implementation of the callback method onDismiss() (depends if you try to remove the item from the adapter). But I think that's one of the most common implementations.
I've just restored mDownPosition = ListView.INVALID_POSITION; just after calling mCallbacks.onDismiss(mListView, dismissPositions); in order to restore mDownPosition so I can avoid dismissing a view with a stale position when this happens.
This is just a quick fix to avoid the crash, I might be missing some other stuff. Please let me know if there's a better way to solve the issue.
Regards,
Gerlac