android implementation of #1488

This commit is contained in:
Peter Staev
2016-02-05 18:52:12 +02:00
parent c0682fce80
commit b290d01664
3 changed files with 66 additions and 2 deletions

View File

@ -108,7 +108,14 @@ export class ActionBar extends view.View implements dts.ActionBar {
} }
get _childrenCount(): number { get _childrenCount(): number {
return this.titleView ? 1 : 0; let actionViewsCount = 0;
this._actionItems.getItems().forEach((actionItem) => {
if (actionItem.actionView) {
actionViewsCount++;
}
});
return actionViewsCount + (this.titleView ? 1 : 0);
} }
constructor() { constructor() {
@ -157,6 +164,12 @@ export class ActionBar extends view.View implements dts.ActionBar {
if (this.titleView) { if (this.titleView) {
callback(this.titleView); callback(this.titleView);
} }
this.actionItems.getItems().forEach((actionItem) => {
if (actionItem.actionView) {
callback(actionItem.actionView);
}
});
} }
public _isEmpty(): boolean { public _isEmpty(): boolean {
@ -263,7 +276,29 @@ export class ActionItem extends bindable.Bindable implements dts.ActionItem {
"visibility", "ActionItem", new dependencyObservable.PropertyMetadata(enums.Visibility.visible, null, ActionItem.onItemChanged)); "visibility", "ActionItem", new dependencyObservable.PropertyMetadata(enums.Visibility.visible, null, ActionItem.onItemChanged));
private _actionBar: ActionBar; private _actionBar: ActionBar;
private _actionView: view.View;
get actionView(): view.View {
return this._actionView;
}
set actionView(value: view.View) {
if (this._actionView !== value) {
ensureStyle();
if (this._actionView && this._actionBar) {
this._actionBar._removeView(this._actionView);
this._actionView.style._resetValue(style.horizontalAlignmentProperty, dependencyObservable.ValueSource.Inherited);
this._actionView.style._resetValue(style.verticalAlignmentProperty, dependencyObservable.ValueSource.Inherited);
}
this._actionView = value;
this._addActionViewToActionBar();
if (this._actionBar) {
this._actionBar.update();
}
}
}
get text(): string { get text(): string {
return this._getValue(ActionItem.textProperty); return this._getValue(ActionItem.textProperty);
} }
@ -297,6 +332,7 @@ export class ActionItem extends bindable.Bindable implements dts.ActionItem {
this._actionBar = value; this._actionBar = value;
if (this._actionBar) { if (this._actionBar) {
this.bindingContext = this._actionBar.bindingContext; this.bindingContext = this._actionBar.bindingContext;
this._addActionViewToActionBar();
} }
} }
} }
@ -318,6 +354,19 @@ export class ActionItem extends bindable.Bindable implements dts.ActionItem {
menuItem.actionBar.update(); menuItem.actionBar.update();
} }
} }
private _addActionViewToActionBar() {
if (this._actionView && !this._actionView._isAddedToNativeVisualTree && this._actionBar) {
ensureStyle();
this._actionView.style._setValue(style.horizontalAlignmentProperty, enums.HorizontalAlignment.center, dependencyObservable.ValueSource.Inherited);
this._actionView.style._setValue(style.verticalAlignmentProperty, enums.VerticalAlignment.center, dependencyObservable.ValueSource.Inherited);
this._actionBar._addView(this._actionView);
}
}
public _addChildFromBuilder(name: string, value: any) {
this.actionView = value;
}
} }
export function isVisible(item: dts.ActionItem) { export function isVisible(item: dts.ActionItem) {

View File

@ -266,7 +266,17 @@ export class ActionBar extends common.ActionBar {
var item = <ActionItem>items[i]; var item = <ActionItem>items[i];
var menuItem = menu.add(android.view.Menu.NONE, item._getItemId(), android.view.Menu.NONE, item.text + ""); var menuItem = menu.add(android.view.Menu.NONE, item._getItemId(), android.view.Menu.NONE, item.text + "");
if (item.android.systemIcon) { if (item.actionView && item.actionView.android) {
menuItem.setActionView(item.actionView.android);
// Note: When using a custom action view the toolbar's MenuItemClickListener is not triggered!
menuItem.getActionView().setOnClickListener(new android.view.View.OnClickListener({
onClick:
function () {
item._raiseTap();
}
}));
}
else if (item.android.systemIcon) {
// Try to look in the system resources. // Try to look in the system resources.
let systemResourceId = getSystemResourceId(item.android.systemIcon); let systemResourceId = getSystemResourceId(item.android.systemIcon);
if (systemResourceId) { if (systemResourceId) {

View File

@ -118,6 +118,11 @@ declare module "ui/action-bar" {
*/ */
icon: string; icon: string;
/**
* Gets or sets the custom action view of the action item.
*/
actionView: view.View;
/** /**
* Gets or sets the visibility of the action item. * Gets or sets the visibility of the action item.
*/ */