Update RecyclerView scroll listener API This change adds RecyclerView as a first parameter to the scroll related callbacks. It also fxies a bug where scroll callback was being called w/ the intended scroll amount instead of the real scroll amount. I also changed it to be an abstract class instead of an interface to make future changes easier. Bug: 16054949 Change-Id: I99a1f91dcd442c1ac3ad1ed7dbf4598d1cee9c28
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/DetailsOverviewRowPresenter.java b/v17/leanback/src/android/support/v17/leanback/widget/DetailsOverviewRowPresenter.java index 6a5e6bc..923c677 100644 --- a/v17/leanback/src/android/support/v17/leanback/widget/DetailsOverviewRowPresenter.java +++ b/v17/leanback/src/android/support/v17/leanback/widget/DetailsOverviewRowPresenter.java
@@ -98,10 +98,10 @@ new RecyclerView.OnScrollListener() { @Override - public void onScrollStateChanged(int newState) { + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { } @Override - public void onScrolled(int dx, int dy) { + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { checkFirstAndLastPosition(true); } };
diff --git a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java index 6befc0c..56a4658 100644 --- a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java +++ b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
@@ -650,7 +650,7 @@ stopScroll(); } if (mScrollListener != null) { - mScrollListener.onScrollStateChanged(state); + mScrollListener.onScrollStateChanged(this, state); } mLayout.onScrollStateChanged(state); } @@ -796,16 +796,17 @@ */ void scrollByInternal(int x, int y) { int overscrollX = 0, overscrollY = 0; + int hresult = 0, vresult = 0; consumePendingUpdateOperations(); if (mAdapter != null) { eatRequestLayout(); mRunningLayoutOrScroll = true; if (x != 0) { - final int hresult = mLayout.scrollHorizontallyBy(x, mRecycler, mState); + hresult = mLayout.scrollHorizontallyBy(x, mRecycler, mState); overscrollX = x - hresult; } if (y != 0) { - final int vresult = mLayout.scrollVerticallyBy(y, mRecycler, mState); + vresult = mLayout.scrollVerticallyBy(y, mRecycler, mState); overscrollY = y - vresult; } if (supportsChangeAnimations()) { @@ -839,8 +840,8 @@ considerReleasingGlowsOnScroll(x, y); pullGlows(overscrollX, overscrollY); } - if (mScrollListener != null && (x != 0 || y != 0)) { - mScrollListener.onScrolled(x, y); + if (mScrollListener != null && (hresult != 0 || vresult != 0)) { + mScrollListener.onScrolled(this, hresult, vresult); } if (!awakenScrollBars()) { invalidate(); @@ -2491,6 +2492,8 @@ final int y = scroller.getCurrY(); final int dx = x - mLastFlingX; final int dy = y - mLastFlingY; + int hresult = 0; + int vresult = 0; mLastFlingX = x; mLastFlingY = y; int overscrollX = 0, overscrollY = 0; @@ -2498,11 +2501,11 @@ eatRequestLayout(); mRunningLayoutOrScroll = true; if (dx != 0) { - final int hresult = mLayout.scrollHorizontallyBy(dx, mRecycler, mState); + hresult = mLayout.scrollHorizontallyBy(dx, mRecycler, mState); overscrollX = dx - hresult; } if (dy != 0) { - final int vresult = mLayout.scrollVerticallyBy(dy, mRecycler, mState); + vresult = mLayout.scrollVerticallyBy(dy, mRecycler, mState); overscrollY = dy - vresult; } if (supportsChangeAnimations()) { @@ -2565,8 +2568,8 @@ } } - if (mScrollListener != null && (x != 0 || y != 0)) { - mScrollListener.onScrolled(dx, dy); + if (mScrollListener != null && (hresult != 0 || vresult != 0)) { + mScrollListener.onScrolled(RecyclerView.this, hresult, vresult); } if (!awakenScrollBars()) { @@ -5651,9 +5654,25 @@ * * @see RecyclerView#setOnScrollListener(OnScrollListener) */ - public interface OnScrollListener { - public void onScrollStateChanged(int newState); - public void onScrolled(int dx, int dy); + abstract static public class OnScrollListener { + /** + * Callback method to be invoked when RecyclerView's scroll state changes. + * + * @param recyclerView The RecyclerView whose scroll state has changed. + * @param newState The updated scroll state. One of {@link #SCROLL_STATE_IDLE}, + * {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}. + */ + public void onScrollStateChanged(RecyclerView recyclerView, int newState){} + + /** + * Callback method to be invoked when the RecyclerView has been scrolled. This will be + * called after the scroll has completed. + * + * @param recyclerView The RecyclerView which scrolled. + * @param dx The amount of horizontal scroll. + * @param dy The amount of vertical scroll. + */ + public void onScrolled(RecyclerView recyclerView, int dx, int dy){} } /**