From 723961957d9e906062951bd68d9a3d4faec32753 Mon Sep 17 00:00:00 2001 From: Brad Martin Date: Fri, 9 Nov 2018 22:38:22 -0600 Subject: [PATCH] closes #84 --- src/fab.android.ts | 57 ++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/fab.android.ts b/src/fab.android.ts index 5ff53f2..849fb9d 100644 --- a/src/fab.android.ts +++ b/src/fab.android.ts @@ -10,14 +10,12 @@ import { rippleColorProperty } from './fab-common'; -declare var android: any; - export class Fab extends FloatingActionButtonBase { private _androidViewId: number; - private _android: any; + private _android: android.support.design.widget.FloatingActionButton; public static tapEvent = 'tap'; - get android(): any { + get android(): android.support.design.widget.FloatingActionButton { return this.nativeView; } @@ -25,27 +23,18 @@ export class Fab extends FloatingActionButtonBase { this._android = new android.support.design.widget.FloatingActionButton( this._context ); - const that = new WeakRef(this); - this._android.setOnClickListener( - new android.view.View.OnClickListener({ - get owner() { - return that.get(); - }, - - onClick: function(v) { - if (this.owner) { - this.owner._emit('tap'); - } - } - }) - ); - return this._android; } public initNativeView() { this._androidViewId = android.view.View.generateViewId(); this.nativeView.setId(this._androidViewId); + initializeClickListener(); + const clickListener = new ClickListener(this); + this.nativeView.setOnClickListener(clickListener); + (this.nativeView).clickListener = clickListener; + // setting scale type statically for now - can add configuration options with next release after confirming fixes for other icons sizes + this.android.setScaleType(android.widget.ImageView.ScaleType.CENTER); } [backgroundColorProperty.getDefault](): android.content.res.ColorStateList { @@ -112,3 +101,33 @@ export class Fab extends FloatingActionButtonBase { } } } + +interface ClickListener { + new (owner: FloatingActionButtonBase): android.view.View.OnClickListener; +} + +let ClickListener: ClickListener; + +function initializeClickListener(): void { + if (ClickListener) { + return; + } + + @Interfaces([android.view.View.OnClickListener]) + class ClickListenerImpl extends java.lang.Object + implements android.view.View.OnClickListener { + constructor(public owner: FloatingActionButtonBase) { + super(); + return global.__native(this); + } + + public onClick(v: android.view.View): void { + const owner = this.owner; + if (owner) { + (owner)._emit('tap'); + } + } + } + + ClickListener = ClickListenerImpl; +}