-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathInvertedScrollView.java
More file actions
80 lines (66 loc) · 2.41 KB
/
InvertedScrollView.java
File metadata and controls
80 lines (66 loc) · 2.41 KB
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
package chat.rocket.reactnative.scroll;
import android.view.View;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.views.scroll.ReactScrollView;
import java.util.ArrayList;
import java.util.Collections;
// When a FlatList is inverted (inverted={true}), React Native uses scaleY: -1 transform which
// visually inverts the list but Android still reports children in array order. This view overrides
// addChildrenForAccessibility to reverse the order so TalkBack matches the visual order, and also
// adjusts keyboard/D-pad focus navigation to behave like a non-inverted list.
public class InvertedScrollView extends ReactScrollView {
private boolean mIsInvertedVirtualizedList = false;
public InvertedScrollView(ReactContext context) {
super(context);
}
// Set whether this ScrollView is used for an inverted virtualized list. When true, we reverse the
// accessibility traversal order to match the visual order.
public void setIsInvertedVirtualizedList(boolean isInverted) {
mIsInvertedVirtualizedList = isInverted;
}
@Override
public View focusSearch(View focused, int direction) {
if (mIsInvertedVirtualizedList) {
switch (direction) {
case View.FOCUS_DOWN:
direction = View.FOCUS_UP;
break;
case View.FOCUS_UP:
direction = View.FOCUS_DOWN;
break;
case View.FOCUS_FORWARD:
direction = View.FOCUS_BACKWARD;
break;
case View.FOCUS_BACKWARD:
direction = View.FOCUS_FORWARD;
break;
default:
break;
}
}
return super.focusSearch(focused, direction);
}
@Override
public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
int initialSize = views.size();
super.addFocusables(views, direction, focusableMode);
if (!mIsInvertedVirtualizedList) {
return;
}
if (direction == View.FOCUS_FORWARD || direction == View.FOCUS_BACKWARD) {
int newSize = views.size();
int addedCount = newSize - initialSize;
if (addedCount > 1) {
java.util.List<View> subList = views.subList(initialSize, newSize);
Collections.reverse(subList);
}
}
}
@Override
public void addChildrenForAccessibility(ArrayList<View> outChildren) {
super.addChildrenForAccessibility(outChildren);
if (mIsInvertedVirtualizedList) {
Collections.reverse(outChildren);
}
}
}