From abd33b38e2a37ca24b70247fff9fe80b9e38b975 Mon Sep 17 00:00:00 2001 From: Stevie Kideckel Date: Wed, 2 Jun 2021 16:57:18 +0000 Subject: [PATCH] Use the LayoutManager as the source of truth for visible positions During animations, the view group child may not correspond to the visible views that appear at the top. The previous logic is kept in the case that the layout manager returns null for any reason, which can happen before layout has occurred for the position. Fix: 189588014 Test: verified locally Change-Id: Ie8b5dcef50287e9e90a21f86e30a1ebcbbcba30f --- .../widget/picker/WidgetsRecyclerView.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java b/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java index e30e245452..090362ba33 100644 --- a/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java +++ b/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java @@ -154,8 +154,25 @@ public class WidgetsRecyclerView extends BaseRecyclerView implements OnItemTouch return -1; } - View child = getChildAt(0); - int rowIndex = getChildPosition(child); + int rowIndex = -1; + View child = null; + + LayoutManager layoutManager = getLayoutManager(); + if (layoutManager instanceof LinearLayoutManager) { + // Use the LayoutManager as the source of truth for visible positions. During + // animations, the view group child may not correspond to the visible views that appear + // at the top. + rowIndex = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); + child = layoutManager.findViewByPosition(rowIndex); + } + + if (child == null) { + // If the layout manager returns null for any reason, which can happen before layout + // has occurred for the position, then look at the child of this view as a ViewGroup. + child = getChildAt(0); + rowIndex = getChildPosition(child); + } + for (int i = 0; i < getChildCount(); i++) { View view = getChildAt(i); if (view instanceof TableLayout) {