This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from hettiger/swipe-compatibility
fix(#29): implement a carousel friendly swipe refresh layout
- Loading branch information
Showing
3 changed files
with
156 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
<page xmlns="http://schemas.nativescript.org/tns.xsd" | ||
xmlns:PullRefresh="nativescript-pulltorefresh" loaded="pageLoaded"> | ||
<page | ||
xmlns="http://schemas.nativescript.org/tns.xsd" | ||
xmlns:PullRefresh="nativescript-pulltorefresh" | ||
xmlns:ns="nativescript-carousel" | ||
loaded="pageLoaded"> | ||
<action-bar title="Pull To Refresh :)" backgroundColor="#2196F3" color="#f1f1f1" /> | ||
<stack-layout> | ||
<PullRefresh:PullToRefresh loaded="swipeLoaded" refresh="refreshList" backgroundColor="#fff000" color="#3489db"> | ||
<list-view items="{{ users }}" backgroundColor="white"> | ||
<list-view.itemTemplate> | ||
<label text="{{ name }}" row="0" col="1" textWrap="true" class="message" /> | ||
</list-view.itemTemplate> | ||
<list-view items="{{ users }}" backgroundColor="white" itemTemplateSelector="$index === 0 ? 'carousel' : 'label'"> | ||
<list-view.itemTemplates> | ||
<template key="carousel"> | ||
<GridLayout rows="200" columns="*"> | ||
<ns:Carousel height="100%" width="100%" pageChanged="myChangeEvent" pageTapped="mySelectedEvent" indicatorColor="#fff000" finite="true" bounce="false" showIndicator="true" verticalAlignment="top" android:indicatorAnimation="swap" color="white"> | ||
<ns:CarouselItem id="slide1" backgroundColor="#b3cde0" verticalAlignment="middle"> | ||
<Label text="Slide 1" backgroundColor="#50000000" horizontalAlignment="center"/> | ||
</ns:CarouselItem> | ||
<ns:CarouselItem id="slide2" backgroundColor="#6497b1" verticalAlignment="middle"> | ||
<Label text="Slide 2" backgroundColor="#50000000" horizontalAlignment="center"/> | ||
</ns:CarouselItem> | ||
<ns:CarouselItem id="slide3" backgroundColor="#005b96" verticalAlignment="middle"> | ||
<Label text="Slide 3" backgroundColor="#50000000" horizontalAlignment="center"/> | ||
</ns:CarouselItem> | ||
<ns:CarouselItem id="slide4" backgroundColor="#03396c" verticalAlignment="middle"> | ||
<Label text="Slide 4" backgroundColor="#50000000" horizontalAlignment="center"/> | ||
</ns:CarouselItem> | ||
</ns:Carousel> | ||
</GridLayout> | ||
</template> | ||
<template key="label"> | ||
<label text="{{ name }}" row="0" col="1" textWrap="true" class="message" /> | ||
</template> | ||
</list-view.itemTemplates> | ||
</list-view> | ||
</PullRefresh:PullToRefresh> | ||
</stack-layout> | ||
</page> | ||
</page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,122 @@ | ||
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" /> | ||
|
||
import { Color } from 'tns-core-modules/color'; | ||
import { | ||
PullToRefreshBase, | ||
backgroundColorProperty, | ||
colorProperty, | ||
refreshingProperty | ||
} from './pulltorefresh-common'; | ||
|
||
export * from './pulltorefresh-common'; | ||
|
||
export class PullToRefresh extends PullToRefreshBase { | ||
private _androidViewId: number; | ||
|
||
public nativeView: any; // android.support.v4.widget.SwipeRefreshLayout; | ||
|
||
get android(): any { | ||
return this.nativeView; // android.support.v4.widget.SwipeRefreshLayout | ||
} | ||
|
||
public createNativeView() { | ||
const swipeRefreshLayout = new (android.support.v4 | ||
.widget as any).SwipeRefreshLayout(this._context); | ||
|
||
if (!this._androidViewId) { | ||
this._androidViewId = android.view.View.generateViewId(); | ||
} | ||
swipeRefreshLayout.setId(this._androidViewId); | ||
|
||
const refreshListener = new TNS_SwipeRefreshListener(new WeakRef(this)); | ||
swipeRefreshLayout.setOnRefreshListener(refreshListener); | ||
(swipeRefreshLayout as any).refreshListener = refreshListener; | ||
|
||
return swipeRefreshLayout; | ||
} | ||
|
||
public initNativeView() { | ||
super.initNativeView(); | ||
|
||
const nativeView = this.nativeView as any; | ||
nativeView.refreshListener.owner = new WeakRef(this); | ||
} | ||
|
||
public disposeNativeView() { | ||
const nativeView = this.nativeView as any; | ||
nativeView.refreshListener.owner = null; | ||
|
||
super.disposeNativeView(); | ||
} | ||
|
||
[refreshingProperty.getDefault](): boolean { | ||
return false; | ||
} | ||
[refreshingProperty.setNative](value: boolean) { | ||
this.nativeView.setRefreshing(value); | ||
} | ||
|
||
[colorProperty.setNative](value: Color | number) { | ||
const color = value instanceof Color ? value.android : value; | ||
this.nativeView.setColorSchemeColors([color]); | ||
} | ||
|
||
[backgroundColorProperty.setNative](value: Color | number) { | ||
const color = value instanceof Color ? value.android : value; | ||
this.nativeView.setProgressBackgroundColorSchemeColor(color); | ||
} | ||
} | ||
|
||
@Interfaces([ | ||
(android.support.v4.widget as any).SwipeRefreshLayout.OnRefreshListener | ||
]) | ||
class TNS_SwipeRefreshListener extends java.lang.Object { | ||
constructor(private owner: WeakRef<PullToRefresh>) { | ||
super(); | ||
|
||
return global.__native(this); | ||
} | ||
|
||
public onRefresh(v) { | ||
const owner = this.owner.get(); | ||
|
||
if (owner) { | ||
owner.refreshing = true; | ||
owner.notify({ | ||
eventName: PullToRefreshBase.refreshEvent, | ||
object: owner | ||
}); | ||
} | ||
} | ||
} | ||
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" /> | ||
|
||
import { Color } from 'tns-core-modules/color'; | ||
import { | ||
PullToRefreshBase, | ||
backgroundColorProperty, | ||
colorProperty, | ||
refreshingProperty | ||
} from './pulltorefresh-common'; | ||
|
||
export * from './pulltorefresh-common'; | ||
|
||
class CarouselFriendlySwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout { | ||
private _touchSlop: number; | ||
private _previousX: number; | ||
|
||
public constructor(context: android.content.Context, attrs: android.util.AttributeSet) { | ||
super(context, attrs); | ||
|
||
this._touchSlop = android.view.ViewConfiguration.get(context).getScaledTouchSlop(); | ||
} | ||
|
||
public onInterceptTouchEvent(event: android.view.MotionEvent): boolean { | ||
switch (event.getAction()) { | ||
case android.view.MotionEvent.ACTION_DOWN: { | ||
this._previousX = android.view.MotionEvent.obtain(event).getX(); | ||
break; | ||
} | ||
case android.view.MotionEvent.ACTION_MOVE: { | ||
const eventX = event.getX(); | ||
const xDifference = Math.abs(eventX - this._previousX); | ||
|
||
if (xDifference > this._touchSlop) { | ||
return false; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
return super.onInterceptTouchEvent(event); | ||
} | ||
} | ||
|
||
export class PullToRefresh extends PullToRefreshBase { | ||
private _androidViewId: number; | ||
|
||
public nativeView: any; // android.support.v4.widget.SwipeRefreshLayout; | ||
|
||
get android(): any { | ||
return this.nativeView; // android.support.v4.widget.SwipeRefreshLayout | ||
} | ||
|
||
public createNativeView() { | ||
const swipeRefreshLayout = new (CarouselFriendlySwipeRefreshLayout as any)(this._context); | ||
|
||
if (!this._androidViewId) { | ||
this._androidViewId = android.view.View.generateViewId(); | ||
} | ||
swipeRefreshLayout.setId(this._androidViewId); | ||
|
||
const refreshListener = new TNS_SwipeRefreshListener(new WeakRef(this)); | ||
swipeRefreshLayout.setOnRefreshListener(refreshListener); | ||
(swipeRefreshLayout as any).refreshListener = refreshListener; | ||
|
||
return swipeRefreshLayout; | ||
} | ||
|
||
public initNativeView() { | ||
super.initNativeView(); | ||
|
||
const nativeView = this.nativeView as any; | ||
nativeView.refreshListener.owner = new WeakRef(this); | ||
} | ||
|
||
public disposeNativeView() { | ||
const nativeView = this.nativeView as any; | ||
nativeView.refreshListener.owner = null; | ||
|
||
super.disposeNativeView(); | ||
} | ||
|
||
[refreshingProperty.getDefault](): boolean { | ||
return false; | ||
} | ||
[refreshingProperty.setNative](value: boolean) { | ||
this.nativeView.setRefreshing(value); | ||
} | ||
|
||
[colorProperty.setNative](value: Color | number) { | ||
const color = value instanceof Color ? value.android : value; | ||
this.nativeView.setColorSchemeColors([color]); | ||
} | ||
|
||
[backgroundColorProperty.setNative](value: Color | number) { | ||
const color = value instanceof Color ? value.android : value; | ||
this.nativeView.setProgressBackgroundColorSchemeColor(color); | ||
} | ||
} | ||
|
||
@Interfaces([ | ||
(android.support.v4.widget as any).SwipeRefreshLayout.OnRefreshListener | ||
]) | ||
class TNS_SwipeRefreshListener extends java.lang.Object { | ||
constructor(private owner: WeakRef<PullToRefresh>) { | ||
super(); | ||
|
||
return global.__native(this); | ||
} | ||
|
||
public onRefresh(v) { | ||
const owner = this.owner.get(); | ||
|
||
if (owner) { | ||
owner.refreshing = true; | ||
owner.notify({ | ||
eventName: PullToRefreshBase.refreshEvent, | ||
object: owner | ||
}); | ||
} | ||
} | ||
} |