React Native对很多native view进行了封装,在Android上的ScrollView就做了一些改动,导致7.0系统上蓝牙键盘tab navigation有问题。问题原因是:
- 在用tab做navigation时,会调用requestChildFocus会判断mIsLayoutDirty字段,如果为true,会等下次layout的时候再进行scroll。
@Override
public void requestChildFocus(View child, View focused) {
if (focused.getRevealOnFocusHint()) {
if (!mIsLayoutDirty) {
scrollToChild(focused);
} else {
// The child may not be laid out yet, we can't compute the scroll yet
mChildToScrollTo = focused;
}
}
super.requestChildFocus(child, focused);
}
- 在ScrollView的onLayout方法中,会对mIsLayoutDirty进行处理,然后进行scroll操作。
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
mIsLayoutDirty = false;
// Give a child focus if it needs it
if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) {
scrollToChild(mChildToScrollTo);
}
- RN封装了ScrollView,自己实现了一个ReactScrollView,Override了onLayout方法,忽略了mIsLayoutDirty方法。
这就导致了在RN里面,ScrollView无法响应tab的navigation导致的Scroll操作。解决办法就是自己写一个ScrollView继承自ReactScrollView,Override requestChildFocus方法。
@Override
public void requestChildFocus(View child, View focused) {
if (focused != null) {
scrollToChild(focused);
}
super.requestChildFocus(child, focused);
};
RN结合accessibility,尤其是加上蓝牙键盘,坑很大。。。