Enables direct scrolling on the fastscroll bar.

This enables to touch anywhere in the scrollbar to directly scroll to that particular point
without the need of exactly touching the thumb to start fastscrolling.

This feature is currently behind a feature flag LAUNCHER3_DIRECT_SCROLL and enabled by default.

Change-Id: I5b86c6acb71ff0c88476343657da221c7bdc59c3
This commit is contained in:
Mario Bertschler 2016-12-27 15:41:24 -08:00
parent c9c57631a9
commit ee4ee424b4
2 changed files with 46 additions and 21 deletions

View File

@ -28,6 +28,8 @@ import android.view.View;
import android.view.ViewConfiguration;
import android.widget.TextView;
import com.android.launcher3.config.FeatureFlags;
/**
* The track and scrollbar that shows when you scroll the list.
*/
@ -198,6 +200,11 @@ public class BaseRecyclerViewFastScrollBar {
case MotionEvent.ACTION_DOWN:
if (isNearThumb(downX, downY)) {
mTouchOffsetY = downY - mThumbOffsetY;
} else if (FeatureFlags.LAUNCHER3_DIRECT_SCROLL
&& mRv.supportsFastScrolling()
&& isNearScrollBar(downX)) {
calcTouchOffsetAndPrepToFastScroll(downY, lastY);
updateFastScrollSectionNameAndThumbOffset(lastY, y);
}
break;
case MotionEvent.ACTION_MOVE:
@ -207,28 +214,10 @@ public class BaseRecyclerViewFastScrollBar {
if (!mIsDragging && !mIgnoreDragGesture && mRv.supportsFastScrolling() &&
isNearThumb(downX, lastY) &&
Math.abs(y - downY) > config.getScaledTouchSlop()) {
mRv.getParent().requestDisallowInterceptTouchEvent(true);
mIsDragging = true;
if (mCanThumbDetach) {
mIsThumbDetached = true;
}
mTouchOffsetY += (lastY - downY);
animatePopupVisibility(true);
showActiveScrollbar(true);
calcTouchOffsetAndPrepToFastScroll(downY, lastY);
}
if (mIsDragging) {
// Update the fastscroller section name at this touch position
int bottom = mRv.getScrollbarTrackHeight() - mThumbHeight;
float boundedY = (float) Math.max(0, Math.min(bottom, y - mTouchOffsetY));
String sectionName = mRv.scrollToPositionAtProgress(boundedY / bottom);
if (!sectionName.equals(mPopupSectionName)) {
mPopupSectionName = sectionName;
mPopupView.setText(sectionName);
}
animatePopupVisibility(!sectionName.isEmpty());
updatePopupY(lastY);
mLastTouchY = boundedY;
setThumbOffsetY((int) mLastTouchY);
updateFastScrollSectionNameAndThumbOffset(lastY, y);
}
break;
case MotionEvent.ACTION_UP:
@ -245,6 +234,32 @@ public class BaseRecyclerViewFastScrollBar {
}
}
private void calcTouchOffsetAndPrepToFastScroll(int downY, int lastY) {
mRv.getParent().requestDisallowInterceptTouchEvent(true);
mIsDragging = true;
if (mCanThumbDetach) {
mIsThumbDetached = true;
}
mTouchOffsetY += (lastY - downY);
animatePopupVisibility(true);
showActiveScrollbar(true);
}
private void updateFastScrollSectionNameAndThumbOffset(int lastY, int y) {
// Update the fastscroller section name at this touch position
int bottom = mRv.getScrollbarTrackHeight() - mThumbHeight;
float boundedY = (float) Math.max(0, Math.min(bottom, y - mTouchOffsetY));
String sectionName = mRv.scrollToPositionAtProgress(boundedY / bottom);
if (!sectionName.equals(mPopupSectionName)) {
mPopupSectionName = sectionName;
mPopupView.setText(sectionName);
}
animatePopupVisibility(!sectionName.isEmpty());
updatePopupY(lastY);
mLastTouchY = boundedY;
setThumbOffsetY((int) mLastTouchY);
}
public void draw(Canvas canvas) {
if (mThumbOffsetY < 0) {
return;
@ -277,7 +292,7 @@ public class BaseRecyclerViewFastScrollBar {
}
/**
* Returns whether the specified points are near the scroll bar bounds.
* Returns whether the specified point is inside the thumb bounds.
*/
public boolean isNearThumb(int x, int y) {
int left = getDrawLeft();
@ -286,6 +301,14 @@ public class BaseRecyclerViewFastScrollBar {
return mTmpRect.contains(x, y);
}
/**
* Returns whether the specified x position is near the scroll bar.
*/
public boolean isNearScrollBar(int x) {
int left = getDrawLeft();
return x >= left && x <= left + mMaxWidth;
}
private void animatePopupVisibility(boolean visible) {
if (mPopupVisible != visible) {
mPopupVisible = visible;

View File

@ -37,4 +37,6 @@ public final class FeatureFlags {
public static final boolean PULLDOWN_SEARCH = false;
// When enabled the status bar may show dark icons based on the top of the wallpaper.
public static final boolean LIGHT_STATUS_BAR = false;
// When enabled allows to use any point on the fast scrollbar to start dragging.
public static final boolean LAUNCHER3_DIRECT_SCROLL = true;
}