From 8d7bf9487bf3ba5bafd05f0bdc06687485ce630c Mon Sep 17 00:00:00 2001 From: Ben Kwa Date: Mon, 30 Nov 2015 14:39:31 -0800 Subject: [PATCH] Switch to smooth scrolling when zooming to home/end. Smooth scrolling fires off a full complement of scroll state changes, which enables the code to more accurately detect when a scroll is finished. BUG=24865658,25632648 Change-Id: Ib902836fdb4a76612d3bbfc4d30d5b0b249301a5 --- .../dirlist/MultiSelectManager.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java index 5994df952de98..5f317ffb29e68 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java @@ -945,16 +945,27 @@ public final class MultiSelectManager implements View.OnKeyListener { if (vh != null) { vh.itemView.requestFocus(); } else { - // Don't smooth scroll; that taxes the system unnecessarily and makes the scroll - // handling logic below more complicated. See b/24865658. - mView.scrollToPosition(pos); + mView.smoothScrollToPosition(pos); // Set a one-time listener to request focus when the scroll has completed. mView.addOnScrollListener( new RecyclerView.OnScrollListener() { @Override - public void onScrolled(RecyclerView view, int dx, int dy) { - view.findViewHolderForAdapterPosition(pos).itemView.requestFocus(); - view.removeOnScrollListener(this); + public void onScrollStateChanged (RecyclerView view, int newState) { + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + // When scrolling stops, find the item and focus it. + RecyclerView.ViewHolder vh = + view.findViewHolderForAdapterPosition(pos); + if (vh != null) { + vh.itemView.requestFocus(); + } else { + // This might happen in weird corner cases, e.g. if the user is + // scrolling while a delete operation is in progress. In that + // case, just don't attempt to focus the missing item. + Log.w( + TAG, "Unable to focus position " + pos + " after a scroll"); + } + view.removeOnScrollListener(this); + } } }); }