RecyclerView scroll callback fix for 0 to 1
This CL fixes a bug in RecyclerView where it would not call
the scroll callbacks if the number of children changed
from 0 to 1 or 1 to 0.
Bug: 29445923
Change-Id: I4ec6fca71ca4800d17b3cc703b8a72b4b180032f
diff --git a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
index afde49c..2c2d587 100644
--- a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
+++ b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
@@ -3470,8 +3470,8 @@
private void findMinMaxChildLayoutPositions(int[] into) {
final int count = mChildHelper.getChildCount();
if (count == 0) {
- into[0] = 0;
- into[1] = 0;
+ into[0] = NO_POSITION;
+ into[1] = NO_POSITION;
return;
}
int minPositionPreLayout = Integer.MAX_VALUE;
@@ -3494,11 +3494,6 @@
}
private boolean didChildRangeChange(int minPositionPreLayout, int maxPositionPreLayout) {
- int count = mChildHelper.getChildCount();
- if (count == 0) {
- return minPositionPreLayout != 0 || maxPositionPreLayout != 0;
- }
- // get the new min max
findMinMaxChildLayoutPositions(mMinMaxLayoutPositions);
return mMinMaxLayoutPositions[0] != minPositionPreLayout ||
mMinMaxLayoutPositions[1] != maxPositionPreLayout;
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewLayoutTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewLayoutTest.java
index 665236f..be742d0 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewLayoutTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewLayoutTest.java
@@ -967,28 +967,39 @@
}
@Test
- public void scrollCalllbackOnVisibleRangeExpand() throws Throwable {
+ public void scrollCallbackFromEmptyToSome() throws Throwable {
+ scrollCallbackOnVisibleRangeChange(1, new int[]{0, 0}, new int[]{0, 1});
+ }
+
+ @Test
+ public void scrollCallbackOnVisibleRangeExpand() throws Throwable {
scrollCallbackOnVisibleRangeChange(10, new int[]{3, 5}, new int[]{3, 6});
}
@Test
- public void scrollCalllbackOnVisibleRangeShrink() throws Throwable {
+ public void scrollCallbackOnVisibleRangeShrink() throws Throwable {
scrollCallbackOnVisibleRangeChange(10, new int[]{3, 6}, new int[]{3, 5});
}
@Test
- public void scrollCalllbackOnVisibleRangeExpand2() throws Throwable {
+ public void scrollCallbackOnVisibleRangeExpand2() throws Throwable {
scrollCallbackOnVisibleRangeChange(10, new int[]{3, 5}, new int[]{2, 5});
}
@Test
- public void scrollCalllbackOnVisibleRangeShrink2() throws Throwable {
+ public void scrollCallbackOnVisibleRangeShrink2() throws Throwable {
scrollCallbackOnVisibleRangeChange(10, new int[]{3, 6}, new int[]{2, 6});
}
private void scrollCallbackOnVisibleRangeChange(int itemCount, final int[] beforeRange,
final int[] afterRange) throws Throwable {
- RecyclerView recyclerView = new RecyclerView(getActivity());
+ RecyclerView recyclerView = new RecyclerView(getActivity()) {
+ @Override
+ void dispatchLayout() {
+ super.dispatchLayout();
+ ((TestLayoutManager) getLayoutManager()).layoutLatch.countDown();
+ }
+ };
final AtomicBoolean beforeState = new AtomicBoolean(true);
TestLayoutManager tlm = new TestLayoutManager() {
@Override
@@ -996,7 +1007,6 @@
detachAndScrapAttachedViews(recycler);
int[] range = beforeState.get() ? beforeRange : afterRange;
layoutRange(recycler, range[0], range[1]);
- layoutLatch.countDown();
}
};
recyclerView.setLayoutManager(tlm);