Support scrollTo with RecycleViewBackedScrollView.

Differential Revision: D2581381

fb-gh-sync-id: 830f36f4d62a3097fdf3697a94a22441f11f93ef
This commit is contained in:
Krzysztof Magiera 2015-10-26 11:16:29 -07:00 committed by facebook-github-bot-3
parent d4eb8201f1
commit cf35f47c4d
2 changed files with 51 additions and 10 deletions

View File

@ -223,26 +223,37 @@ public class RecyclerViewBackedScrollView extends RecyclerView {
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
ReactListAdapter adapter = (ReactListAdapter) getAdapter();
private int calculateAbsoluteOffset() {
int offsetY = 0;
if (getChildCount() > 0) {
View recyclerViewChild = getChildAt(0);
int childPosition = getChildAdapterPosition(recyclerViewChild);
offsetY = adapter.getTopOffsetForItem(childPosition) - recyclerViewChild.getTop();
offsetY = ((ReactListAdapter) getAdapter()).getTopOffsetForItem(childPosition) -
recyclerViewChild.getTop();
}
return offsetY;
}
/*package*/ void scrollTo(int scrollX, int scrollY, boolean animated) {
int deltaY = scrollY - calculateAbsoluteOffset();
if (animated) {
smoothScrollBy(0, deltaY);
} else {
scrollBy(0, deltaY);
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
ScrollEvent event = new ScrollEvent(
getId(),
SystemClock.uptimeMillis(),
0, /* offsetX = 0, horizontal scrolling only */
offsetY,
calculateAbsoluteOffset(),
getWidth(),
adapter.getTotalChildrenHeight(),
((ReactListAdapter) getAdapter()).getTotalChildrenHeight(),
getWidth(),
getHeight());
((ReactContext) getContext()).getNativeModule(UIManagerModule.class).getEventDispatcher()

View File

@ -2,16 +2,21 @@
package com.facebook.react.views.recyclerview;
import javax.annotation.Nullable;
import android.view.View;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.views.scroll.ReactScrollViewCommandHelper;
/**
* View manager for {@link RecyclerViewBackedScrollView}.
*/
public class RecyclerViewBackedScrollViewManager extends
ViewGroupManager<RecyclerViewBackedScrollView> {
ViewGroupManager<RecyclerViewBackedScrollView>
implements ReactScrollViewCommandHelper.ScrollCommandHandler<RecyclerViewBackedScrollView> {
private static final String REACT_CLASS = "AndroidRecyclerViewBackedScrollView";
@ -46,4 +51,29 @@ public class RecyclerViewBackedScrollViewManager extends
public void removeView(RecyclerViewBackedScrollView parent, View child) {
parent.removeViewFromAdapter(child);
}
/**
* Provides implementation of commands supported by {@link ReactScrollViewManager}
*/
@Override
public void receiveCommand(
RecyclerViewBackedScrollView view,
int commandId,
@Nullable ReadableArray args) {
ReactScrollViewCommandHelper.receiveCommand(this, view, commandId, args);
}
@Override
public void scrollTo(
RecyclerViewBackedScrollView view,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
view.scrollTo(data.mDestX, data.mDestY, true);
}
@Override
public void scrollWithoutAnimationTo(
RecyclerViewBackedScrollView view,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
view.scrollTo(data.mDestX, data.mDestY, false);
}
}