Skip to content

Commit

Permalink
Fixes #47
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Mar 11, 2018
1 parent 2740040 commit 81b3ad2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
22 changes: 14 additions & 8 deletions src/pager.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Pager extends PagerBase {
private _views: Array<any>;
private _transformer;
private _pageListener: any;
_viewMap: Map<number, View>;
_viewMap: Map<string, View>;
public _realizedItems = new Map<android.view.View, View>();
public _realizedTemplates = new Map<string, Map<android.view.View, View>>();
constructor() {
Expand Down Expand Up @@ -294,6 +294,9 @@ export class PagerAdapter extends android.support.v4.view.PagerAdapter {
return global.__native(this);
}

getItemPosition(obj) {
return android.support.v4.view.PagerAdapter.POSITION_NONE;
}
instantiateItem(collection: android.view.ViewGroup, position: number) {
if (!this.owner) {
return null;
Expand All @@ -302,8 +305,8 @@ export class PagerAdapter extends android.support.v4.view.PagerAdapter {
this.owner.notify({ eventName: LOADMOREITEMS, object: this.owner });
}
const template = this.owner._getItemTemplate(position);
if (this.owner._viewMap.has(position)) {
const cachedView = this.owner._viewMap.get(position);
if (this.owner._viewMap.has(`${position}-${template.key}`)) {
const cachedView = this.owner._viewMap.get(`${position}-${template.key}`);
let convertView = cachedView ? cachedView.nativeView : null;
if (convertView) {
// collection.addView(convertView);
Expand All @@ -324,21 +327,24 @@ export class PagerAdapter extends android.support.v4.view.PagerAdapter {
if (!view.parent) {
this.owner._addView(view);
}
this.owner._viewMap.set(position, view);
this.owner._viewMap.set(`${position}-${template.key}`, view);
}

collection.addView(view.nativeView);
return view.nativeView;
}

destroyItem(collection: android.view.ViewGroup, position: number, object) {
if (this.owner._viewMap.has(position)) {
let convertView: any = this.owner._viewMap.get(position)
? this.owner._viewMap.get(position)
const template = this.owner._getItemTemplate(position);
if (this.owner._viewMap.has(`${position}-${template.key}`)) {
let convertView: any = this.owner._viewMap.get(
`${position}-${template.key}`
)
? this.owner._viewMap.get(`${position}-${template.key}`)
: null;
if (convertView && convertView.nativeView) {
collection.removeView(convertView.nativeView);
this.owner._viewMap.delete(position);
this.owner._viewMap.delete(`${position}-${template.key}`);
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/pager.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Pager extends PagerBase {
private heightMeasureSpec: number;
private layoutWidth = 0;
private layoutHeight = 0;
private _viewMap: Map<number, View>;
private _viewMap: Map<string, View>;
private cachedViewControllers: WeakRef<PagerView>[] = [];
private delegate: PagerViewControllerDelegate;
private dataSource: PagerDataSource;
Expand Down Expand Up @@ -148,8 +148,10 @@ export class Pager extends PagerBase {
if (!vc) {
vc = PagerView.initWithOwnerTag(new WeakRef(this), selectedIndex);
let view: View;
let template;
if (this.items && this.items.length) {
view = this._getItemTemplate(selectedIndex).createView();
template = this._getItemTemplate(selectedIndex);
view = template.createView();

let _args: any = notifyForItemAtIndex(
this,
Expand All @@ -172,7 +174,7 @@ export class Pager extends PagerBase {
if (view) {
this.cachedViewControllers[selectedIndex] = new WeakRef(vc);
this._prepareItem(view, selectedIndex);
this._viewMap.set(selectedIndex, view);
this._viewMap.set(`${selectedIndex}-${template.key}`, view);
}
} else {
let lbl = new Label();
Expand Down Expand Up @@ -279,8 +281,8 @@ export class Pager extends PagerBase {
const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
const height = layout.getMeasureSpecSize(heightMeasureSpec);
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);

const view = this._viewMap.get(this.selectedIndex);
const template = this._getItemTemplate(this.selectedIndex);
const view = this._viewMap.get(`${this.selectedIndex}-${template.key}`);
let { measuredWidth, measuredHeight } = View.measureChild(
this,
view,
Expand Down Expand Up @@ -317,8 +319,9 @@ export class Pager extends PagerBase {
super.onLayout(left, top, right, bottom);
this.layoutWidth = right - left;
this.layoutHeight = bottom - top;
if (this._viewMap && this._viewMap.has(this.selectedIndex)) {
const view = this._viewMap.get(this.selectedIndex);
const template = this._getItemTemplate(this.selectedIndex);
if (this._viewMap && this._viewMap.has(`${this.selectedIndex}-${template.key}`)) {
const view = this._viewMap.get(`${this.selectedIndex}-${template.key}`);
View.layoutChild(this, view, 0, 0, this.layoutWidth, this.layoutHeight);
}
}
Expand Down Expand Up @@ -364,7 +367,8 @@ export class Pager extends PagerBase {

private _navigateNativeViewPagerToIndex(fromIndex: number, toIndex: number) {
const vc = this.getViewController(toIndex);
const view = this._viewMap.get(toIndex);
const template = this._getItemTemplate(toIndex);
const view = this._viewMap.get(`${toIndex}-${template.key}`);
this.prepareView(view);
if (!vc) throw new Error('no VC');
const direction =
Expand Down

0 comments on commit 81b3ad2

Please sign in to comment.