Skip to content

Commit

Permalink
support smooth scrolling with offset
Browse files Browse the repository at this point in the history
Summary: Can smooth scroll to child with an added offset

Reviewed By: astreet

Differential Revision: D8682952

fbshipit-source-id: 334406f81d4cb6681442e5648d0c87cffe3967bd
  • Loading branch information
hanli1 authored and facebook-github-bot committed Jun 29, 2018
1 parent 5ae4a44 commit 62fa98e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public void requestFocus(int index) {
}

@Override
public void requestSmoothFocus(int index, SmoothScrollAlignmentType type) {
mTarget.requestSmoothFocus(index, type);
public void requestSmoothFocus(int index, int offset, SmoothScrollAlignmentType type) {
mTarget.requestSmoothFocus(index, offset, type);
maybeLogRequestFocus(index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ void requestFocus(int index) {
}

@UiThread
void requestSmoothFocus(int index, SmoothScrollAlignmentType type) {
void requestSmoothFocus(int index, int offset, SmoothScrollAlignmentType type) {
if (shouldDispatchRequests()) {
mTarget.requestSmoothFocus(index, type);
mTarget.requestSmoothFocus(index, offset, type);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,16 @@ public static void requestFocusWithOffset(
}

public static void requestSmoothFocus(SectionContext c, int index) {
requestSmoothFocus(c, "", index, SmoothScrollAlignmentType.DEFAULT);
requestSmoothFocus(c, "", index, 0, SmoothScrollAlignmentType.DEFAULT);
}

public static void requestSmoothFocus(
SectionContext c, int index, SmoothScrollAlignmentType type) {
requestSmoothFocus(c, "", index, type);
requestSmoothFocus(c, "", index, 0, type);
}

public static void requestSmoothFocus(
SectionContext c, String keyString, int index, SmoothScrollAlignmentType type) {
SectionContext c, String keyString, int index, int offset, SmoothScrollAlignmentType type) {
final Section scopedSection = c.getSectionScope();
final SectionTree sectionTree = c.getSectionTree();

Expand All @@ -391,6 +391,6 @@ public static void requestSmoothFocus(
}

final String globalKey = scopedSection.getGlobalKey() + keyString;
sectionTree.requestSmoothFocus(globalKey, index, type);
sectionTree.requestSmoothFocus(globalKey, index, offset, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public interface Target {
void requestFocus(int index);

/** Request smooth focus on the item with the given index. */
void requestSmoothFocus(int index, SmoothScrollAlignmentType type);
void requestSmoothFocus(int index, int offset, SmoothScrollAlignmentType type);

/**
* Request focus on the item with the given index, plus some additional offset.
Expand Down Expand Up @@ -661,14 +661,18 @@ public void run() {
}

void requestSmoothFocus(
final String globalKey, final int index, final SmoothScrollAlignmentType type) {
final String globalKey,
final int index,
final int offset,
final SmoothScrollAlignmentType type) {
focusRequestOnUiThread(
new Runnable() {
@Override
public void run() {
final SectionLocationInfo sectionLocationInfo = findSectionForKey(globalKey);
checkFocusValidity(sectionLocationInfo, index);
mFocusDispatcher.requestSmoothFocus(sectionLocationInfo.mStartIndex + index, type);
mFocusDispatcher.requestSmoothFocus(
sectionLocationInfo.mStartIndex + index, offset, type);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public void requestFocus(int index) {
}

@Override
public void requestSmoothFocus(int index, SmoothScrollAlignmentType type) {
mRecyclerBinder.scrollSmoothToPosition(index, type);
public void requestSmoothFocus(int index, int offset, SmoothScrollAlignmentType type) {
mRecyclerBinder.scrollSmoothToPosition(index, offset, type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void requestFocus(int index) {
}

@Override
public void requestSmoothFocus(int index, SmoothScrollAlignmentType type) {
public void requestSmoothFocus(int index, int offset, SmoothScrollAlignmentType type) {
requestFocus(index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2301,9 +2301,11 @@ public void scrollToPosition(int position) {
}

@UiThread
public void scrollSmoothToPosition(int position, final SmoothScrollAlignmentType type) {
public void scrollSmoothToPosition(
int position, final int offset, final SmoothScrollAlignmentType type) {
if (mMountedView == null) {
mCurrentFirstVisiblePosition = position;
mCurrentOffset = offset;
return;
}

Expand All @@ -2313,11 +2315,20 @@ public void scrollSmoothToPosition(int position, final SmoothScrollAlignmentType
|| type == SmoothScrollAlignmentType.SNAP_TO_END) {
RecyclerView.SmoothScroller selectedSmoothScroller =
new LinearSmoothScroller(mComponentContext) {
@Override
public int calculateDtToFit(
int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
int result =
super.calculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, snapPreference);
return result + offset;
}

@Override
protected int getVerticalSnapPreference() {
return representation;
}

@Override
protected int getHorizontalSnapPreference() {
return representation;
}
Expand Down

0 comments on commit 62fa98e

Please sign in to comment.