-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRCTPullToRefreshScrollViewManager.java
143 lines (113 loc) · 6.01 KB
/
RCTPullToRefreshScrollViewManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.rntaobao.pullToRefresh.viewManager;
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.annotations.ReactProp;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.rntaobao.pullToRefresh.event.LoadingLayoutScrollPositionChangeEvent;
import com.rntaobao.pullToRefresh.event.OnRefreshStartEvent;
import com.rntaobao.pullToRefresh.event.PullToRefreshStateChangeEvent;
import com.rntaobao.pullToRefresh.view.RCTPullToRefreshScrollView;
import java.util.Map;
import javax.annotation.Nullable;
/**
* {@link RCTPullToRefreshScrollView}与js层的接口
*/
public class RCTPullToRefreshScrollViewManager extends ViewGroupManager<RCTPullToRefreshScrollView> {
private ThemedReactContext context;
private EventDispatcher mEventDispatcher;
private static final String NAME = "RCTPullToRefreshScrollView";
private static final String CUSTOM_DIRECT_EVENT_TYPE_CONSTANTS_FIELD_REGISTRATION_NAME = "registrationName";
private static final String JS_CALLBACK_NAME_PULL_TO_REFRESH_STATE_CHANGE = "onPullToRefreshStateChange";
private static final String JS_CALLBACK_NAME_LOADING_LAYOUT_SCROLL_POSITION_CHANGE = "onLoadingLayoutScrollPositionChange";
private static final String JS_CALLBACK_NAME_ON_REFRESH_START = "onRefreshStart";
private static final int COMMAND_ID_REFRESH_COMPLETE = 1;
@Override
protected RCTPullToRefreshScrollView createViewInstance(ThemedReactContext reactContext) {
context = reactContext;
mEventDispatcher = context.getNativeModule(UIManagerModule.class).getEventDispatcher();
return new RCTPullToRefreshScrollView(reactContext);
}
@Override
public String getName() {
return NAME;
}
/**
* 注意js端传来的prop数值单位是dp,而java层需要的是px
*/
@ReactProp(name = "minDraggedDistanceToRefresh")
public void setMinDraggedDistanceToRefresh(RCTPullToRefreshScrollView v, int distanceDp) {
v.setMinDraggedDistanceToRefreshDp(distanceDp);
}
@Override
protected void addEventEmitters(final ThemedReactContext reactContext, RCTPullToRefreshScrollView view) {
// TODO 需要创建event池以复用event,节省内存
/**
* {@link RCTPullToRefreshScrollView.OnPullToRefreshStateChangeListener}
* 向js端发送{@link com.rntaobao.pullToRefresh.view.RCTPullToRefreshScrollView.State}变化的信息
* */
view.setOnPullToRefreshStateChangeListener(new RCTPullToRefreshScrollView.OnPullToRefreshStateChangeListener() {
@Override
public void onPullToRefreshStateChange(RCTPullToRefreshScrollView v, RCTPullToRefreshScrollView.State state) {
PullToRefreshStateChangeEvent event = new PullToRefreshStateChangeEvent(v.getId(), state);
mEventDispatcher.dispatchEvent(event);
}
});
/**
* {@link com.rntaobao.pullToRefresh.view.RCTPullToRefreshScrollView.OnLoadingLayoutScrollPositionChangeListener}
* 向js端发送其滚动位置变化的信息
* */
view.setOnLoadingLayoutScrollPositionChangeListener(new RCTPullToRefreshScrollView.OnLoadingLayoutScrollPositionChangeListener() {
@Override
public void onLoadingLayoutScrollPositionChange(RCTPullToRefreshScrollView v, float loadingLayoutScrollPositionRatio) {
LoadingLayoutScrollPositionChangeEvent event = new LoadingLayoutScrollPositionChangeEvent(v.getId(), loadingLayoutScrollPositionRatio);
mEventDispatcher.dispatchEvent(event);
}
});
/**
* {@link com.rntaobao.pullToRefresh.view.RCTPullToRefreshScrollView.OnRefreshListener}
* 向js端发送刷新开始的消息
* */
view.setOnRefreshListener(new RCTPullToRefreshScrollView.OnRefreshListener() {
@Override
public void onRefresh(RCTPullToRefreshScrollView v) {
OnRefreshStartEvent event = new OnRefreshStartEvent(v.getId());
mEventDispatcher.dispatchEvent(event);
}
});
}
@Nullable
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Map pullToRefreshStateChangeEventFieldMap = MapBuilder.of(CUSTOM_DIRECT_EVENT_TYPE_CONSTANTS_FIELD_REGISTRATION_NAME, JS_CALLBACK_NAME_PULL_TO_REFRESH_STATE_CHANGE);
Map loadingLayoutScrollPositionChangeEventFieldMap = MapBuilder.of(CUSTOM_DIRECT_EVENT_TYPE_CONSTANTS_FIELD_REGISTRATION_NAME, JS_CALLBACK_NAME_LOADING_LAYOUT_SCROLL_POSITION_CHANGE);
Map onRefreshStartEventFieldMap = MapBuilder.of(CUSTOM_DIRECT_EVENT_TYPE_CONSTANTS_FIELD_REGISTRATION_NAME, JS_CALLBACK_NAME_ON_REFRESH_START);
Map eventsToFieldsMap = MapBuilder.of(PullToRefreshStateChangeEvent.EVENT_NAME, pullToRefreshStateChangeEventFieldMap, LoadingLayoutScrollPositionChangeEvent.EVENT_NAME, loadingLayoutScrollPositionChangeEventFieldMap, OnRefreshStartEvent.EVENT_NAME, onRefreshStartEventFieldMap);
return eventsToFieldsMap;
}
@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of("notifyRefreshComplete", COMMAND_ID_REFRESH_COMPLETE);
}
@Override
public void receiveCommand(RCTPullToRefreshScrollView root, int commandId, @Nullable ReadableArray args) {
switch (commandId) {
case COMMAND_ID_REFRESH_COMPLETE:
root.notifyRefreshComplete();
break;
default:
break;
}
}
/**
* 需要java来负责本层的layout而非js,以便支持{@link com.rntaobao.pullToRefresh.view.RCTPullToRefreshLoadingLayout}效果所必须的负的padding设置
* 而css不支持负的padding
*/
@Override
public boolean needsCustomLayoutForChildren() {
return true;
}
}