-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathReactTabLayoutManager.java
More file actions
179 lines (146 loc) · 5.15 KB
/
ReactTabLayoutManager.java
File metadata and controls
179 lines (146 loc) · 5.15 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.reactnativeandroiddesignsupport;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import java.util.ArrayList;
import java.util.Map;
import javax.annotation.Nullable;
public class ReactTabLayoutManager extends ViewGroupManager<TabLayout> {
public ReadableArray mTabs = null;
private ArrayList<TabLayout.Tab> mCustomTabs = new ArrayList<>();
@Override
public String getName() {
return "RCTTabLayoutAndroid";
}
@Override
public TabLayout createViewInstance(ThemedReactContext context) {
mCustomTabs.clear();
return new TabLayout(context);
}
public boolean needsCustomLayoutForChildren() {
return true;
}
@Override
public void addView(TabLayout view, View child, int index) {
TabLayout.Tab tab = view.newTab();
tab.setCustomView(child);
view.addTab(tab);
mCustomTabs.add(tab);
}
@Override
public int getChildCount(TabLayout view) {
// mCustomTabs contains tabs that actually have custom views.
// This is called by NativeViewHierarchyManager.dropView() during cleanup
// which then will call getChildAt() for each index.
// If there are no custom views, then this will correctly return 0
// since t.getCustomView() will return null if there is not a custom view for a tab.
return mCustomTabs.size();
}
@Override
public View getChildAt(TabLayout view, int index) {
TabLayout.Tab t = view.getTabAt(index);
return t.getCustomView();
}
@Override
public void removeViewAt(TabLayout view, int index) {
view.removeTabAt(index);
}
@Override
public void removeAllViews(TabLayout view) {
view.removeAllTabs();
}
public static final int COMMAND_SET_VIEW_PAGER = 1;
@Override
public Map<String,Integer> getCommandsMap() {
return MapBuilder.of(
"setViewPager",
COMMAND_SET_VIEW_PAGER);
}
@Override
public void receiveCommand(TabLayout view, int commandType, @Nullable ReadableArray args) {
Assertions.assertNotNull(view);
Assertions.assertNotNull(args);
switch (commandType) {
case COMMAND_SET_VIEW_PAGER: {
int viewPagerId = args.getInt(0);
ViewPager viewPager = (ViewPager) view.getRootView().findViewById(viewPagerId);
view.setupWithViewPager(viewPager);
ReadableArray tabs = args.getArray(1);
if (tabs != null) {
view.removeAllTabs();
this.populateTabLayoutWithTabs(view, tabs);
}
else if (!mCustomTabs.isEmpty()) {
view.removeAllTabs();
this.populateTabLayoutWithCustomTabs(view);
}
return;
}
default:
throw new JSApplicationIllegalArgumentException(String.format(
"Unsupported command %d received by %s.",
commandType,
getClass().getSimpleName()));
}
}
@ReactProp(name = "tabs")
public void setTabs(TabLayout view, ReadableArray tabs) {
view.removeAllTabs();
this.mTabs = tabs;
this.populateTabLayoutWithTabs(view, tabs);
}
@ReactProp(name = "normalColor", customType = "Color")
public void setNormalColor(TabLayout view, int color) {
int selectedColor = view.getTabTextColors().getColorForState(ReactTabLayout.getSelectedStateSet(), color);
view.setTabTextColors(color, selectedColor);
}
@ReactProp(name = "selectedColor", customType = "Color")
public void setSelectedColor(TabLayout view, int color) {
int normalColor = view.getTabTextColors().getColorForState(ReactTabLayout.getEmptyStateSet(), color);
view.setTabTextColors(normalColor, color);
}
@ReactProp(name = "selectedTabIndicatorColor", customType = "Color")
public void setSelectedTabIndicatorColor(TabLayout view, int color) {
view.setSelectedTabIndicatorColor(color);
}
@ReactProp(name = "tabMode")
public void setTabMode(TabLayout view, String mode) {
if ("fixed".equals(mode)) {
view.setTabMode(TabLayout.MODE_FIXED);
} else if ("scrollable".equals(mode)) {
view.setTabMode(TabLayout.MODE_SCROLLABLE);
}
}
private void populateTabLayoutWithTabs(TabLayout view, ReadableArray tabs) {
try {
int tabSize = tabs.size();
for (int i=0; i<tabSize; i++) {
ReadableMap tabMap = tabs.getMap(i);
TabLayout.Tab tab = view.newTab();
if (tabMap.hasKey("text")) tab.setText(tabMap.getString("text"));
// TODO: Deal with icons, etc.
view.addTab(tab);
}
} catch (Exception e) {
// TODO: Handle Exception
}
}
private void populateTabLayoutWithCustomTabs(TabLayout view) {
try {
int tabSize = mCustomTabs.size();
for (int i=0; i < tabSize; i++) {
view.addTab(mCustomTabs.get(i));
}
} catch (Exception e) {
// TODO: Handle Exception
}
}
}