Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #87 from gsmedley/master
Browse files Browse the repository at this point in the history
Add optional ripple effect (Android only)
  • Loading branch information
bradmartin authored May 29, 2018
2 parents c76e1aa + 0422377 commit cd8b37d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ An attribute to control the 'border-radius' of the card.
An attribute to set the elevation of the card. This will increase the 'drop-shadow' of the card.
There can be some performance impact when using a very high elevation value.

* **ripple** _optional_

Set to 'true' to show a ripple when the card is tapped. Tap event handlers in the card content will prevent the ripple.


#### iOS

* **shadowOffsetWidth** _optional_
Expand Down
11 changes: 11 additions & 0 deletions src/cardview-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export class CardViewCommon extends ContentView {
* Set the shadow opacity of the card view. ** Valid only when running on iOS **
*/
shadowOpacity: number;

/**
* Gets or set the ripple setting.
*/
ripple: boolean;
}

export const radiusProperty = new Property<CardViewCommon, number>({
Expand All @@ -64,6 +69,12 @@ export const elevationProperty = new Property<CardViewCommon, number>({
});
elevationProperty.register(CardViewCommon);

export const rippleProperty = new Property<CardViewCommon, boolean>({
name: 'ripple',
valueConverter: value => value == "true"
});
rippleProperty.register(CardViewCommon);

export const shadowRadiusProperty = new Property<CardViewCommon, number>({
name: 'shadowRadius',
valueConverter: value => +value
Expand Down
28 changes: 27 additions & 1 deletion src/cardview.android.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />

import { Color } from 'tns-core-modules/color';
var application = require("tns-core-modules/application");
import {
backgroundColorProperty,
backgroundInternalProperty,
CardViewCommon,
elevationProperty,
radiusProperty
radiusProperty,
rippleProperty
} from './cardview-common';

export class CardView extends CardViewCommon {
Expand All @@ -25,6 +27,30 @@ export class CardView extends CardViewCommon {
this.nativeView.setCardElevation(value);
}

[rippleProperty.setNative](value: boolean) {

if( !value ){
this.nativeView.setClickable(false);
} else {
let attr = java.lang.Class.forName("android.support.v7.appcompat.R$attr");
let field = attr.getField("selectableItemBackground");

if( field ) {
let resId = field.getInt(null);

let attrs = Array.create("int", 1);
attrs[0] = resId
let activity = application.android.foregroundActivity;
let typedValue = activity.obtainStyledAttributes(attrs);
let selectedItemDrawable = typedValue.getDrawable(0);

this.nativeView.setForeground(selectedItemDrawable);
this.nativeView.setClickable(true);
}
}
}


public createNativeView() {
return new (android.support.v7.widget as any).CardView(this._context);
}
Expand Down
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ export class CardView extends ContentView {
* Set the shadow opacity of the card view. ** Valid only when running on iOS **
*/
shadowOpacity: number;

/**
* Gets or set the ripple setting.
*/
ripple: boolean;
}
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-cardview",
"version": "3.0.1",
"version": "3.1.0",
"description": "A NativeScript plugin for Material Design CardView component.",
"main": "cardview",
"typings": "index.d.ts",
Expand Down

0 comments on commit cd8b37d

Please sign in to comment.