Open source SwipeRefreshLayoutAndroid

Reviewed By: mkonicek

Differential Revision: D2679605

fb-gh-sync-id: 7f3e7384b37f29002ddd8cb7a4567fa96c76f047
This commit is contained in:
Konstantin Raev
2015-11-24 08:50:39 -08:00
committed by facebook-github-bot-8
parent e55d70789c
commit 37f81341a0
9 changed files with 403 additions and 1 deletions

View File

@@ -39,6 +39,7 @@ import com.facebook.react.views.textinput.ReactTextInputManager;
import com.facebook.react.views.toolbar.ReactToolbarManager;
import com.facebook.react.views.view.ReactViewManager;
import com.facebook.react.views.viewpager.ReactViewPagerManager;
import com.facebook.react.views.swiperefresh.SwipeRefreshLayoutManager;
/**
* Package defining basic modules and view managers.
@@ -78,6 +79,7 @@ public class MainReactPackage implements ReactPackage {
new ReactViewManager(),
new ReactViewPagerManager(),
new ReactTextInlineImageViewManager(),
new ReactVirtualTextViewManager());
new ReactVirtualTextViewManager(),
new SwipeRefreshLayoutManager());
}
}

View File

@@ -0,0 +1,35 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.views.swiperefresh;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.events.NativeGestureUtil;
/**
* Basic extension of {@link SwipeRefreshLayout} with ReactNative-specific functionality.
*/
public class ReactSwipeRefreshLayout extends SwipeRefreshLayout {
public ReactSwipeRefreshLayout(ReactContext reactContext) {
super(reactContext);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (super.onInterceptTouchEvent(ev)) {
NativeGestureUtil.notifyNativeGestureStarted(this, ev);
return true;
}
return false;
}
}

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.views.swiperefresh;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;
public class RefreshEvent extends Event<RefreshEvent> {
protected RefreshEvent(int viewTag, long timestampMs) {
super(viewTag, timestampMs);
}
@Override
public String getEventName() {
return "topRefresh";
}
@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), null);
}
}

View File

@@ -0,0 +1,106 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.views.swiperefresh;
import javax.annotation.Nullable;
import java.util.Map;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
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.UIManagerModule;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewProps;
/**
* ViewManager for {@link ReactSwipeRefreshLayout} which allows the user to "pull to refresh" a
* child view. Emits an {@code onRefresh} event when this happens.
*/
public class SwipeRefreshLayoutManager extends ViewGroupManager<ReactSwipeRefreshLayout> {
@Override
protected ReactSwipeRefreshLayout createViewInstance(ThemedReactContext reactContext) {
return new ReactSwipeRefreshLayout(reactContext);
}
@Override
public String getName() {
return "AndroidSwipeRefreshLayout";
}
@ReactProp(name = ViewProps.ENABLED, defaultBoolean = true)
public void setEnabled(ReactSwipeRefreshLayout view, boolean enabled) {
view.setEnabled(enabled);
}
@ReactProp(name = "colors")
public void setColors(ReactSwipeRefreshLayout view, @Nullable ReadableArray colors) {
if (colors != null) {
int[] colorValues = new int[colors.size()];
for (int i = 0; i < colors.size(); i++) {
colorValues[i] = colors.getInt(i);
}
view.setColorSchemeColors(colorValues);
} else {
view.setColorSchemeColors();
}
}
@ReactProp(name = "progressBackgroundColor", defaultInt = Color.TRANSPARENT, customType = "Color")
public void setProgressBackgroundColor(ReactSwipeRefreshLayout view, int color) {
view.setProgressBackgroundColorSchemeColor(color);
}
@ReactProp(name = "size", defaultInt = SwipeRefreshLayout.DEFAULT)
public void setSize(ReactSwipeRefreshLayout view, int size) {
view.setSize(size);
}
@ReactProp(name = "refreshing")
public void setRefreshing(ReactSwipeRefreshLayout view, boolean refreshing) {
view.setRefreshing(refreshing);
}
@Override
protected void addEventEmitters(
final ThemedReactContext reactContext,
final ReactSwipeRefreshLayout view) {
view.setOnRefreshListener(
new OnRefreshListener() {
@Override
public void onRefresh() {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new RefreshEvent(view.getId(), SystemClock.uptimeMillis()));
}
});
}
@Nullable
@Override
public Map<String, Object> getExportedViewConstants() {
return MapBuilder.<String, Object>of(
"SIZE",
MapBuilder.of("DEFAULT", SwipeRefreshLayout.DEFAULT, "LARGE", SwipeRefreshLayout.LARGE));
}
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
return MapBuilder.<String, Object>builder()
.put("topRefresh", MapBuilder.of("registrationName", "onRefresh"))
.build();
}
}