Android原生可以通过设置android:nextFocusForward等指定导航顺序。RN没有内置支持,可以通过发送event的方式进行指定。
<View ref={(ref: any) => (this.rootView = ref)}>
...
</View>
然后在componentDidMount的时候发送event给native。
componentDidMount() {
if (context.isAndroid() && this.rootView !== 'undefined') {
var focusOrderList = [];
... // Generate focus order list here
var UIManager = require('UIManager');
var rootViewHandle = findNodeHandle(this.rootView);
UIManager.dispatchViewManagerCommand(
rootViewHandle,
ViewCommands.NEXT_FOCUS_COMMAND,
focusOrderList,
);
}
}
自己实现一个ViewManager,继承自ReactViewManager,把默认的ReactViewManager替换掉,然后实现receiveCommand方法。
@Override
public void receiveCommand(final ReactViewGroup root, int commandId, @Nullable ReadableArray args) {
super.receiveCommand(root, commandId, args);
if (commandId == CMD_NEXT_FOCUS) {
if (args != null) {
int length = args.size();
for (int i = 0; i < length - 1;i++) {
View firstView = root.getRootView().findViewWithTag(args.getString(i));
View nextFocusView = root.getRootView().findViewWithTag(args.getString(i+1));
if (firstView != null && nextFocusView != null) {
firstView.setNextFocusForwardId(nextFocusView.getId());
}
}
}
}
}
网友评论