mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-09 09:12:06 +08:00
Add setPageWithoutAnimation
Summary: In some cases it's desirable to set the page in the ViewPager without animating it -- we have this in ScrollView with `scrollWithoutAnimationTo`. This PR adds `setPageWithoutAnimation` on ViewPager. cc ide kmagiera Closes https://github.com/facebook/react-native/pull/3621 Reviewed By: svcscm Differential Revision: D2652056 Pulled By: mkonicek fb-gh-sync-id: 6f1f38558c41ffdd863c0ebb2f046c75b5c0392c
This commit is contained in:
committed by
facebook-github-bot-3
parent
88a92f8f52
commit
50b8b00768
@@ -133,6 +133,12 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setCurrentItemFromJs(int item, boolean animated) {
|
||||
mIsCurrentItemFromJs = true;
|
||||
setCurrentItem(item, animated);
|
||||
mIsCurrentItemFromJs = false;
|
||||
}
|
||||
|
||||
/*package*/ void addViewToAdapter(View child, int index) {
|
||||
getAdapter().addView(child, index);
|
||||
}
|
||||
@@ -148,10 +154,4 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
|
||||
/*package*/ View getViewFromAdapter(int index) {
|
||||
return getAdapter().getViewAt(index);
|
||||
}
|
||||
|
||||
/*package*/ void setCurrentItemFromJs(int item) {
|
||||
mIsCurrentItemFromJs = true;
|
||||
setCurrentItem(item);
|
||||
mIsCurrentItemFromJs = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,11 +13,14 @@ import java.util.Map;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.common.MapBuilder;
|
||||
import com.facebook.react.uimanager.ReactProp;
|
||||
import com.facebook.react.uimanager.ThemedReactContext;
|
||||
import com.facebook.react.uimanager.ViewGroupManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Instance of {@link ViewManager} that provides native {@link ViewPager} view.
|
||||
*/
|
||||
@@ -25,6 +28,9 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager> {
|
||||
|
||||
private static final String REACT_CLASS = "AndroidViewPager";
|
||||
|
||||
public static final int COMMAND_SET_PAGE = 1;
|
||||
public static final int COMMAND_SET_PAGE_WITHOUT_ANIMATION = 2;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return REACT_CLASS;
|
||||
@@ -35,12 +41,6 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager> {
|
||||
return new ReactViewPager(reactContext);
|
||||
}
|
||||
|
||||
@ReactProp(name = "selectedPage")
|
||||
public void setSelectedPage(ReactViewPager view, int page) {
|
||||
// TODO(8496821): Handle selectedPage property cleanup correctly, now defaults to 0
|
||||
view.setCurrentItemFromJs(page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsCustomLayoutForChildren() {
|
||||
return true;
|
||||
@@ -54,6 +54,39 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager> {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String,Integer> getCommandsMap() {
|
||||
return MapBuilder.of(
|
||||
"setPage",
|
||||
COMMAND_SET_PAGE,
|
||||
"setPageWithoutAnimation",
|
||||
COMMAND_SET_PAGE_WITHOUT_ANIMATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveCommand(
|
||||
ReactViewPager viewPager,
|
||||
int commandType,
|
||||
@Nullable ReadableArray args) {
|
||||
Assertions.assertNotNull(viewPager);
|
||||
Assertions.assertNotNull(args);
|
||||
switch (commandType) {
|
||||
case COMMAND_SET_PAGE: {
|
||||
viewPager.setCurrentItemFromJs(args.getInt(0), true);
|
||||
return;
|
||||
}
|
||||
case COMMAND_SET_PAGE_WITHOUT_ANIMATION: {
|
||||
viewPager.setCurrentItemFromJs(args.getInt(0), false);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Unsupported command %d received by %s.",
|
||||
commandType,
|
||||
getClass().getSimpleName()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addView(ReactViewPager parent, View child, int index) {
|
||||
parent.addViewToAdapter(child, index);
|
||||
|
||||
Reference in New Issue
Block a user