forked from race604/ZhiHuDaily-React-Native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
311 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict'; | ||
|
||
var React = require('react-native'); | ||
var { | ||
requireNativeComponent, | ||
PropTypes, | ||
StyleSheet, | ||
View, | ||
} = React; | ||
|
||
var createReactNativeComponentClass = require('createReactNativeComponentClass'); | ||
var ReactNativeViewAttributes = require('ReactNativeViewAttributes'); | ||
var RCTUIManager = require('NativeModules').UIManager; | ||
|
||
var NativeMethodsMixin = require('NativeMethodsMixin'); | ||
|
||
var RK_SWIPE_REF = 'swiperefreshlayout'; | ||
var INNERVIEW_REF = 'innerView'; | ||
|
||
var SwipeRefreshLayoutAndroid = React.createClass({ | ||
propTypes: { | ||
onRefresh: PropTypes.func, | ||
}, | ||
|
||
mixins: [NativeMethodsMixin], | ||
|
||
getInnerViewNode: function() { | ||
return this.refs[INNERVIEW_REF].getInnerViewNode(); | ||
}, | ||
|
||
render: function() { | ||
var childrenWrapper = | ||
<View ref={INNERVIEW_REF} style={styles.mainSubview} collapsable={false}> | ||
{this.props.children} | ||
</View>; | ||
return ( | ||
<AndroidSwipeRefreshLayout | ||
{...this.props} | ||
ref={RK_SWIPE_REF} | ||
style={styles.base} | ||
onRefresh={this._onRefresh}> | ||
{childrenWrapper} | ||
</AndroidSwipeRefreshLayout> | ||
); | ||
}, | ||
|
||
_onRefresh: function() { | ||
if (this.props.onRefresh) { | ||
this.props.onRefresh(); | ||
} | ||
}, | ||
|
||
startRefresh: function() { | ||
RCTUIManager.dispatchViewManagerCommand( | ||
this._getSwipeRefreshLayoutHandle(), | ||
RCTUIManager.AndroidSwipeRefreshLayout.Commands.startRefresh, | ||
null | ||
); | ||
}, | ||
|
||
finishRefresh: function() { | ||
RCTUIManager.dispatchViewManagerCommand( | ||
this._getSwipeRefreshLayoutHandle(), | ||
RCTUIManager.AndroidSwipeRefreshLayout.Commands.finishRefresh, | ||
null | ||
); | ||
}, | ||
|
||
_getSwipeRefreshLayoutHandle: function() { | ||
return React.findNodeHandle(this.refs[RK_SWIPE_REF]); | ||
}, | ||
}); | ||
|
||
var styles = StyleSheet.create({ | ||
base: { | ||
flex: 1, | ||
}, | ||
mainSubview: { | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
bottom: 0, | ||
}, | ||
}); | ||
|
||
var AndroidSwipeRefreshLayout = createReactNativeComponentClass({ | ||
validAttributes: ReactNativeViewAttributes.UIView, | ||
uiViewClassName: 'AndroidSwipeRefreshLayout', | ||
}); | ||
|
||
module.exports = SwipeRefreshLayoutAndroid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
android/app/src/main/java/com/race604/react/view/swiperefresh/ReactSwipeRefreshLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.race604.react.view.swiperefresh; | ||
|
||
import android.support.v4.widget.SwipeRefreshLayout; | ||
import android.util.Log; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ScrollView; | ||
|
||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.react.uimanager.events.NativeGestureUtil; | ||
|
||
/** | ||
* Created by Jing on 15/9/30. | ||
*/ | ||
public class ReactSwipeRefreshLayout extends SwipeRefreshLayout { | ||
|
||
private static final String TAG = "NativeView"; | ||
private ScrollView mScrollChild = null; | ||
|
||
public ReactSwipeRefreshLayout(ReactContext context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
public boolean onInterceptTouchEvent(MotionEvent ev) { | ||
|
||
if (mScrollChild != null && mScrollChild.getScrollY() > 0) { | ||
return false; | ||
} | ||
|
||
if (super.onInterceptTouchEvent(ev)) { | ||
NativeGestureUtil.notifyNativeGestureStarted(this, ev); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | ||
super.onLayout(changed, left, top, right, bottom); | ||
mScrollChild = findScrollChild(this); | ||
} | ||
|
||
private ScrollView findScrollChild(View root) { | ||
View child = root; | ||
while (child instanceof ViewGroup) { | ||
child = ((ViewGroup) child).getChildAt(0); | ||
if (child instanceof ScrollView) { | ||
return (ScrollView) child; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...app/src/main/java/com/race604/react/view/swiperefresh/ReactSwipeRefreshLayoutManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.race604.react.view.swiperefresh; | ||
|
||
import android.os.SystemClock; | ||
import android.support.v4.widget.SwipeRefreshLayout; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.common.MapBuilder; | ||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.UIManagerModule; | ||
import com.facebook.react.uimanager.ViewGroupManager; | ||
import com.facebook.react.uimanager.events.EventDispatcher; | ||
import com.race604.react.view.swiperefresh.event.RefreshEvent; | ||
|
||
import java.util.Map; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Created by Jing on 15/9/30. | ||
*/ | ||
public class ReactSwipeRefreshLayoutManager extends ViewGroupManager<ReactSwipeRefreshLayout> { | ||
|
||
private static final String REACT_CLASS = "AndroidSwipeRefreshLayout"; | ||
private static final String TAG = "NativeView"; | ||
|
||
public static final int START_REFRESH = 1; | ||
public static final int FINISH_REFRESH = 2; | ||
|
||
@Override | ||
public String getName() { | ||
return REACT_CLASS; | ||
} | ||
|
||
@Override | ||
protected void addEventEmitters(ThemedReactContext reactContext, ReactSwipeRefreshLayout view) { | ||
view.setOnRefreshListener( | ||
new SwipeRefreshEventEmitter( | ||
view, | ||
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher())); | ||
} | ||
|
||
@Override | ||
protected ReactSwipeRefreshLayout createViewInstance(ThemedReactContext reactContext) { | ||
return new ReactSwipeRefreshLayout(reactContext); | ||
} | ||
|
||
@Override | ||
public boolean needsCustomLayoutForChildren() { | ||
return true; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Map<String, Integer> getCommandsMap() { | ||
return MapBuilder.of("startRefresh", START_REFRESH, "finishRefresh", FINISH_REFRESH); | ||
} | ||
|
||
@Override | ||
public void receiveCommand(ReactSwipeRefreshLayout root, int commandId, | ||
@Nullable ReadableArray args) { | ||
switch (commandId) { | ||
case START_REFRESH: | ||
root.setRefreshing(true); | ||
return; | ||
case FINISH_REFRESH: | ||
root.setRefreshing(false); | ||
return; | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Map getExportedCustomDirectEventTypeConstants() { | ||
return MapBuilder.of( | ||
RefreshEvent.EVENT_NAME, MapBuilder.of("registrationName", "onRefresh")); | ||
} | ||
|
||
@Override | ||
public void addView(ReactSwipeRefreshLayout parent, View child, int index) { | ||
if (getChildCount(parent) >= 2) { | ||
throw new | ||
JSApplicationIllegalArgumentException("The SwipeRefreshLayout cannot have more than one children"); | ||
} | ||
|
||
parent.addView(child, index); | ||
} | ||
|
||
public static class SwipeRefreshEventEmitter implements SwipeRefreshLayout.OnRefreshListener { | ||
|
||
private final SwipeRefreshLayout mSwipeRefreshLayout; | ||
private final EventDispatcher mEventDispatcher; | ||
|
||
public SwipeRefreshEventEmitter(SwipeRefreshLayout layout, EventDispatcher dispatcher) { | ||
mSwipeRefreshLayout = layout; | ||
mEventDispatcher = dispatcher; | ||
} | ||
|
||
@Override | ||
public void onRefresh() { | ||
mEventDispatcher.dispatchEvent( | ||
new RefreshEvent(mSwipeRefreshLayout.getId(), SystemClock.uptimeMillis())); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
android/app/src/main/java/com/race604/react/view/swiperefresh/event/RefreshEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.race604.react.view.swiperefresh.event; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.uimanager.events.Event; | ||
import com.facebook.react.uimanager.events.RCTEventEmitter; | ||
|
||
/** | ||
* Created by Jing on 15/10/1. | ||
*/ | ||
public class RefreshEvent extends Event<RefreshEvent> { | ||
|
||
public static final String EVENT_NAME = "topSwipeRefresh"; | ||
|
||
public RefreshEvent(int viewTag, long timestampMs) { | ||
super(viewTag, timestampMs); | ||
} | ||
|
||
@Override | ||
public String getEventName() { | ||
return EVENT_NAME; | ||
} | ||
|
||
@Override | ||
public short getCoalescingKey() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void dispatch(RCTEventEmitter rctEventEmitter) { | ||
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), Arguments.createMap()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ce604/rn/component/ObservableWebView.java → ...react/view/webview/ObservableWebView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...604/rn/component/ReactWebViewManager.java → ...act/view/webview/ReactWebViewManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters