Fix OrientationHelper NPE
Fixes: 63386256
Test: RecyclerViewCacheTest#prefetchAfterOrientationChange
Change-Id: I730903f056b51cbd7465077aaf7355966655ae24
diff --git a/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java b/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java
index a229846..a12e9a8 100644
--- a/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java
+++ b/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java
@@ -1310,6 +1310,7 @@
return;
}
+ ensureLayoutState();
final int layoutDirection = delta > 0 ? LayoutState.LAYOUT_END : LayoutState.LAYOUT_START;
final int absDy = Math.abs(delta);
updateLayoutState(layoutDirection, absDy, true, state);
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewCacheTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewCacheTest.java
index 620953a..1267fdb 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewCacheTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewCacheTest.java
@@ -298,6 +298,41 @@
}
@Test
+ public void prefetchAfterOrientationChange() {
+ LinearLayoutManager layout = new LinearLayoutManager(getContext(),
+ LinearLayoutManager.VERTICAL, false);
+ mRecyclerView.setLayoutManager(layout);
+
+ // 100x100 pixel views
+ mRecyclerView.setAdapter(new RecyclerView.Adapter() {
+ @Override
+ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ View view = new View(getContext());
+ view.setMinimumWidth(100);
+ view.setMinimumHeight(100);
+ assertTrue(mRecyclerView.isComputingLayout());
+ return new RecyclerView.ViewHolder(view) {};
+ }
+
+ @Override
+ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
+
+ @Override
+ public int getItemCount() {
+ return 100;
+ }
+ });
+
+ layout(100, 100);
+
+ layout.setOrientation(LinearLayoutManager.HORIZONTAL);
+
+ // Prefetch an item after changing orientation, before layout - shouldn't crash
+ mRecyclerView.mPrefetchRegistry.setPrefetchVector(1, 1);
+ mRecyclerView.mGapWorker.prefetch(RecyclerView.FOREVER_NS);
+ }
+
+ @Test
public void prefetchDrag() {
// event dispatch requires a parent
ViewGroup parent = new FrameLayout(getContext());