From b6c3075deb8f414f20005743a5bf5eee55825e66 Mon Sep 17 00:00:00 2001 From: "Warren R. Bank" Date: Thu, 19 May 2022 15:50:34 -0700 Subject: [PATCH] add workaround to prevent crashing due to a bug in RecyclerView references: =========== https://issuetracker.google.com/issues/37136189 https://stackoverflow.com/questions/43221847 --- .../recycler_view/RecyclerViewCallback.java | 31 ++++++++++++++----- .../recycler_view/RecyclerViewCallback.java | 31 ++++++++++++++----- android-studio-project/constants.gradle | 4 +-- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/list_view/recycler_view/RecyclerViewCallback.java b/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/list_view/recycler_view/RecyclerViewCallback.java index ab1a363..031a33a 100644 --- a/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/list_view/recycler_view/RecyclerViewCallback.java +++ b/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/list_view/recycler_view/RecyclerViewCallback.java @@ -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; } } diff --git a/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/main/recycler_view/RecyclerViewCallback.java b/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/main/recycler_view/RecyclerViewCallback.java index f11b04d..aa2ce1d 100644 --- a/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/main/recycler_view/RecyclerViewCallback.java +++ b/android-studio-project/RTSP-IPCam-Viewer/src/main/java/com/github/warren_bank/rtsp_ipcam_viewer/main/recycler_view/RecyclerViewCallback.java @@ -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; } } diff --git a/android-studio-project/constants.gradle b/android-studio-project/constants.gradle index 0a3e043..d8d2826 100644 --- a/android-studio-project/constants.gradle +++ b/android-studio-project/constants.gradle @@ -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