1
- import { Application , Color , CoreTypes , Font , Frame , ImageSource , Utils , View , getTransformedText } from '@nativescript/core' ;
1
+ import { Application , Color , CoreTypes , Font , ImageSource , Utils , getTransformedText } from '@nativescript/core' ;
2
2
import { TabContentItem } from '../tab-content-item' ;
3
3
import { getIconSpecSize , itemsProperty , selectedIndexProperty , tabStripProperty } from '../tab-navigation-base' ;
4
4
import { TabStrip } from '../tab-strip' ;
5
5
import { TabStripItem } from '../tab-strip-item' ;
6
- import { TabNavigationBase , TabsPosition , animationEnabledProperty , offscreenTabLimitProperty , swipeEnabledProperty } from './index-common' ;
6
+ import { TabNavigationBase , TabsPosition , offscreenTabLimitProperty , swipeEnabledProperty } from './index-common' ;
7
7
export * from './index-common' ;
8
8
export { TabContentItem , TabStrip , TabStripItem } ;
9
9
@@ -27,7 +27,6 @@ let PagerAdapter: PagerAdapter;
27
27
// eslint-disable-next-line no-redeclare
28
28
let PageChangeCallback : PageChangeCallback ;
29
29
let appResources : android . content . res . Resources ;
30
- // let AttachStateChangeListener: any;
31
30
32
31
function getTabById ( id : number ) : TabNavigation {
33
32
const ref = tabs . find ( ( ref ) => {
@@ -75,7 +74,7 @@ function initializeNativeClasses() {
75
74
class TabFragmentImplementation extends org . nativescript . widgets . FragmentBase {
76
75
private owner : TabNavigation ;
77
76
private index : number ;
78
- private backgroundBitmap : android . graphics . Bitmap = null ;
77
+ // private backgroundBitmap: android.graphics.Bitmap = null;
79
78
80
79
constructor ( ) {
81
80
super ( ) ;
@@ -111,7 +110,6 @@ function initializeNativeClasses() {
111
110
}
112
111
113
112
public onDestroyView ( ) {
114
- console . log ( 'TabFragmentImplementation' , 'onDestroyView' , this . index ) ;
115
113
// const hasRemovingParent = this.getRemovingParentFragment();
116
114
117
115
// // Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments.
@@ -160,54 +158,30 @@ function initializeNativeClasses() {
160
158
161
159
@NativeClass
162
160
class FragmentPagerAdapter extends androidx . viewpager2 . adapter . FragmentStateAdapter {
163
- public items : TabContentItem [ ] ;
164
- private mCurTransaction : androidx . fragment . app . FragmentTransaction ;
165
- private mCurrentPrimaryItem : androidx . fragment . app . Fragment ;
166
-
167
161
constructor ( public owner : WeakRef < TabNavigation > , fragmentActivity : androidx . fragment . app . FragmentActivity ) {
168
162
super ( fragmentActivity ) ;
169
-
170
163
return global . __native ( this ) ;
171
164
}
172
165
173
166
getItemCount ( ) {
174
- const items = this . items ;
175
-
176
- return items ? items . length : 0 ;
167
+ const owner = this . owner ?. get ( ) ;
168
+ if ( ! owner ) {
169
+ return 0 ;
170
+ }
171
+ return owner . items . length ;
177
172
}
178
-
179
- // getPageTitle(index: number) {
180
- // const items = this.items;
181
- // if (index < 0 || index >= items.length) {
182
- // return '';
183
- // }
184
-
185
- // return ''; // items[index].title;
186
- // }
187
173
createFragment ( position : number ) : androidx . fragment . app . Fragment {
188
174
const owner = this . owner ?. get ( ) ;
189
175
if ( ! owner ) {
190
176
return null ;
191
177
}
192
-
193
- const itemId = this . getItemId ( position ) ;
194
- // const name = makeFragmentName(itemId);
195
-
196
178
const fragment : androidx . fragment . app . Fragment = TabFragmentImplementation . newInstance ( owner . _domId , position ) ;
197
-
198
- const tabItems = owner . items ;
199
- const tabItem = tabItems ? tabItems [ position ] : null ;
200
- if ( tabItem ) {
201
- tabItem . canBeLoaded = true ;
202
- }
203
-
204
179
return fragment ;
205
180
}
206
181
}
207
182
208
183
PagerAdapter = FragmentPagerAdapter ;
209
184
PageChangeCallback = PageChangeCallbackImpl ;
210
- // AttachStateChangeListener = new AttachListener();
211
185
appResources = Application . android . context . getResources ( ) ;
212
186
}
213
187
@@ -238,17 +212,8 @@ function setElevation(grid: org.nativescript.widgets.GridLayout, tabsBar: androi
238
212
239
213
export const tabs = new Array < WeakRef < TabNavigation > > ( ) ;
240
214
241
- function iterateIndexRange ( index : number , eps : number , lastIndex : number , callback : ( i ) => void ) {
242
- const rangeStart = Math . max ( 0 , index - eps ) ;
243
- const rangeEnd = Math . min ( index + eps , lastIndex ) ;
244
- for ( let i = rangeStart ; i <= rangeEnd ; i ++ ) {
245
- callback ( i ) ;
246
- }
247
- }
248
-
249
215
//TODO: move to abstract
250
216
export abstract class TabNavigation < T extends android . view . ViewGroup = any > extends TabNavigationBase {
251
- // export abstract class TabNavigation<T extends com.nativescript.material.core.TabsBar> extends TabNavigationBase {
252
217
nativeViewProtected : org . nativescript . widgets . GridLayout ;
253
218
protected mTabsBar : T ;
254
219
protected mViewPager : androidx . viewpager2 . widget . ViewPager2 ;
@@ -289,9 +254,6 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
289
254
const context : android . content . Context = this . _context ;
290
255
const nativeView = new org . nativescript . widgets . GridLayout ( context ) ;
291
256
const viewPager = new androidx . viewpager2 . widget . ViewPager2 ( context ) ;
292
- //dirty trick to be able to disable swipe
293
- // const recyclerView = viewPager as any as androidx.recyclerview.widget.RecyclerView;
294
- // recyclerView.addOnItemTouchListener();
295
257
const lp = new org . nativescript . widgets . CommonLayoutParams ( ) ;
296
258
lp . row = 1 ;
297
259
if ( this . tabsPosition === 'top' ) {
@@ -313,7 +275,6 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
313
275
( viewPager as any ) . adapter = adapter ;
314
276
this . mViewPager = viewPager ;
315
277
setElevation ( nativeView , null , this . tabsPosition ) ;
316
-
317
278
if ( this . tabStrip ) {
318
279
this . handleTabStripChanged ( nativeView , null , this . tabStrip ) ;
319
280
}
@@ -344,6 +305,7 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
344
305
}
345
306
nativeView . addView ( tabsBar ) ;
346
307
nativeView [ 'tabsBar' ] = tabsBar ;
308
+ this . setTabStripItems ( newTabStrip ?. items || null ) ;
347
309
}
348
310
}
349
311
public onTabStripChanged ( oldTabStrip : TabStrip , newTabStrip : TabStrip ) {
@@ -388,13 +350,14 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
388
350
public onLoaded ( ) : void {
389
351
super . onLoaded ( ) ;
390
352
391
- if ( this . _originalBackground ) {
392
- this . backgroundColor = null ;
393
- this . backgroundColor = this . _originalBackground ;
394
- this . _originalBackground = null ;
395
- }
353
+ // if (this._originalBackground) {
354
+ // console.log('setting original background', this._originalBackground);
355
+ // this.backgroundColor = null;
356
+ // this.backgroundColor = this._originalBackground;
357
+ // this._originalBackground = null;
358
+ // }
396
359
397
- this . setItems ( this . items as any ) ;
360
+ // this.setItems(this.items as any);
398
361
399
362
if ( this . tabStrip ) {
400
363
this . setTabStripItems ( this . tabStrip . items ) ;
@@ -404,13 +367,13 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
404
367
public onUnloaded ( ) : void {
405
368
super . onUnloaded ( ) ;
406
369
407
- this . setItems ( null ) ;
408
- this . setTabStripItems ( null ) ;
370
+ // this.setItems(null);
371
+ // this.setTabStripItems(null);
409
372
410
373
// TODO: needed ?
411
- this . items . forEach ( ( item , i ) => {
412
- item . unloadView ( item . content ) ;
413
- } ) ;
374
+ // this.items.forEach((item, i) => {
375
+ // item.unloadView(item.content);
376
+ // });
414
377
}
415
378
416
379
private shouldUpdateAdapter ( items : TabContentItem [ ] ) {
@@ -447,15 +410,13 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
447
410
}
448
411
449
412
private setItems ( items : TabContentItem [ ] ) {
413
+ if ( items && items . length ) {
414
+ items . forEach ( ( item : TabContentItem , i ) => {
415
+ item . index = i ;
416
+ } ) ;
417
+ }
418
+ this . _loadUnloadTabItems ( this . selectedIndex ) ;
450
419
if ( this . shouldUpdateAdapter ( items ) ) {
451
- ( this . mPagerAdapter as any ) . items = items ;
452
-
453
- if ( items && items . length ) {
454
- items . forEach ( ( item : TabContentItem , i ) => {
455
- ( item as any ) . index = i ;
456
- } ) ;
457
- }
458
-
459
420
this . mPagerAdapter . notifyDataSetChanged ( ) ;
460
421
}
461
422
}
@@ -664,8 +625,7 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
664
625
if ( ! tabStripItem . nativeViewProtected ) {
665
626
return ;
666
627
}
667
- const itemColor = tabStripItem . index === this . selectedIndex ? this . mSelectedItemColor : this . mUnSelectedItemColor ;
668
-
628
+ const itemColor = tabStripItem . index === this . selectedIndex ? this . mSelectedItemColor : tabStripItem . style . color || this . mUnSelectedItemColor ;
669
629
// set label color
670
630
if ( itemColor ) {
671
631
tabStripItem . nativeViewProtected . setTextColor ( itemColor . android || null ) ;
0 commit comments