Skip to content

Commit

Permalink
add workaround to prevent crashing due to a bug in RecyclerView
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed May 19, 2022
1 parent 469c07c commit b6c3075
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,32 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);

int fromPos = draggingFromPosition;
int toPos = draggingToPosition;

draggingFromPosition = C.INDEX_UNSET;
draggingToPosition = C.INDEX_UNSET;

if (
(draggingFromPosition < videos.size()) &&
(draggingToPosition < videos.size()) &&
(draggingFromPosition != C.INDEX_UNSET)
(fromPos != C.INDEX_UNSET) &&
(toPos != C.INDEX_UNSET) &&
(fromPos < videos.size()) &&
(toPos < videos.size()) &&
(fromPos != toPos)
) {
videos.add(draggingToPosition, videos.remove(draggingFromPosition));
adapter.notifyDataSetChanged();
videos.add(toPos, videos.remove(fromPos));

if (!recyclerView.isComputingLayout() && (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE)) {
adapter.notifyDataSetChanged();
}
else {
recyclerView.post(new Runnable(){
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
draggingFromPosition = C.INDEX_UNSET;
draggingToPosition = C.INDEX_UNSET;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,33 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);

int fromPos = draggingFromPosition;
int toPos = draggingToPosition;

draggingFromPosition = C.INDEX_UNSET;
draggingToPosition = C.INDEX_UNSET;

if (
(draggingFromPosition < videos.size()) &&
(draggingToPosition < videos.size()) &&
(draggingFromPosition != C.INDEX_UNSET)
(fromPos != C.INDEX_UNSET) &&
(toPos != C.INDEX_UNSET) &&
(fromPos < videos.size()) &&
(toPos < videos.size()) &&
(fromPos != toPos)
) {
videos.add(draggingToPosition, videos.remove(draggingFromPosition));
adapter.notifyDataSetChanged();
videos.add(toPos, videos.remove(fromPos));
RecyclerViewAdapter.saveVideos(adapter);

if (!recyclerView.isComputingLayout() && (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE)) {
adapter.notifyDataSetChanged();
}
else {
recyclerView.post(new Runnable(){
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
draggingFromPosition = C.INDEX_UNSET;
draggingToPosition = C.INDEX_UNSET;
}
}
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
releaseVersionCode = Integer.parseInt("002030016", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.03.00-16API'
releaseVersionCode = Integer.parseInt("002030116", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.03.01-16API'
javaVersion = JavaVersion.VERSION_1_8
minSdkVersion = 16
targetSdkVersion = 29
Expand Down

0 comments on commit b6c3075

Please sign in to comment.