Use AppCompat SearchView (#4371)

* Use AppCompat SearchView

* GetNative elements for appcompat

* Classes renamed

* test modified
This commit is contained in:
Alexander Vakrilov
2017-06-14 14:11:04 +03:00
committed by GitHub
parent 4319ca5fc5
commit 1d20845b7c
3 changed files with 37 additions and 23 deletions

View File

@ -5,11 +5,10 @@
<NavigationButton text="Go Back" android.systemIcon="ic_menu_back" tap="onNavBtnTap"/>
</ActionBar>
</Page.actionBar>
<StackLayout>
<!--The background-color shouldn't be inherit but in Android it is-->
<SearchBar id="bg-color" hint="bg-color" text="" textFieldHintColor="green" />
<SearchBar class="color" hint="color" text="color" />
<SearchBar hint="Hint Text" id="searchBar" textFieldHintColor="#FFFFFF" style="color:#FFFFFF;background-color: #795548;height:60" />
<SearchBar hint="Hint Text" textFieldHintColor="#FFFFFF" textFieldBackgroundColor="red" style="background-color:green;" />
</StackLayout>
<GridLayout rows="auto auto auto auto *">
<SearchBar row="0" id="bg-color" hint="bg-color" text="" textFieldHintColor="green" />
<SearchBar row="1" class="color" hint="color" text="color" />
<SearchBar row="2" hint="Hint Text" id="searchBar" textFieldHintColor="#FFFFFF" style="color:#FFFFFF;background-color: #795548;height:60" />
<SearchBar row="3" hint="Hint Text" textFieldHintColor="#FFFFFF" textFieldBackgroundColor="red" style="background-color:green;" />
</GridLayout>
</Page>

View File

@ -4,9 +4,10 @@ import * as utils from "tns-core-modules/utils/utils";
function getTextView(bar: android.widget.SearchView): android.widget.TextView {
if (bar) {
var id = bar.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
const pkgName = bar.getContext().getPackageName();
var id = bar.getContext().getResources().getIdentifier("search_src_text", "id", pkgName);
if (id) {
return <android.widget.TextView> bar.findViewById(id);
return <android.widget.TextView>bar.findViewById(id);
}
}

View File

@ -11,11 +11,11 @@ const SEARCHTEXT = Symbol("searchText");
const QUERY = Symbol("query");
interface QueryTextListener {
new (owner: SearchBar): android.widget.SearchView.OnQueryTextListener;
new (owner: SearchBar): android.support.v7.widget.SearchView.OnQueryTextListener;
}
interface CloseListener {
new (owner: SearchBar): android.widget.SearchView.OnCloseListener;
new (owner: SearchBar): android.support.v7.widget.SearchView.OnCloseListener;
}
let QueryTextListener: QueryTextListener;
@ -26,8 +26,8 @@ function initializeNativeClasses(): void {
return;
}
@Interfaces([android.widget.SearchView.OnQueryTextListener])
class QueryTextListenerImpl extends java.lang.Object implements android.widget.SearchView.OnQueryTextListener {
@Interfaces([android.support.v7.widget.SearchView.OnQueryTextListener])
class CompatQueryTextListenerImpl extends java.lang.Object implements android.support.v7.widget.SearchView.OnQueryTextListener {
constructor(private owner: SearchBar) {
super();
return global.__native(this);
@ -58,8 +58,8 @@ function initializeNativeClasses(): void {
}
}
@Interfaces([android.widget.SearchView.OnCloseListener])
class CloseListenerImpl extends java.lang.Object implements android.widget.SearchView.OnCloseListener {
@Interfaces([android.support.v7.widget.SearchView.OnCloseListener])
class CompatCloseListenerImpl extends java.lang.Object implements android.support.v7.widget.SearchView.OnCloseListener {
constructor(private owner: SearchBar) {
super();
return global.__native(this);
@ -71,12 +71,14 @@ function initializeNativeClasses(): void {
}
}
QueryTextListener = QueryTextListenerImpl;
CloseListener = CloseListenerImpl;
QueryTextListener = CompatQueryTextListenerImpl;
CloseListener = CompatCloseListenerImpl;
}
export class SearchBar extends SearchBarBase {
nativeView: android.widget.SearchView;
nativeView: android.support.v7.widget.SearchView;
private _searchTextView: android.widget.TextView;
private _searchPlate: android.widget.LinearLayout;
public dismissSoftInput() {
ad.dismissSoftInput(this.nativeView);
@ -93,7 +95,7 @@ export class SearchBar extends SearchBarBase {
public createNativeView() {
initializeNativeClasses();
const nativeView = new android.widget.SearchView(this._context);
const nativeView = new android.support.v7.widget.SearchView(this._context)
nativeView.setIconified(false);
const queryTextListener = new QueryTextListener(this);
@ -118,6 +120,8 @@ export class SearchBar extends SearchBarBase {
const nativeView: any = this.nativeView;
nativeView.closeListener.owner = null;
nativeView.queryTextListener.owner = null;
this._searchPlate = null;
this._searchTextView = null;
super.disposeNativeView();
}
@ -208,12 +212,22 @@ export class SearchBar extends SearchBarBase {
}
private _getTextView(): android.widget.TextView {
const id = this.nativeView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
return <android.widget.TextView>this.nativeView.findViewById(id);
if (!this._searchTextView) {
const pkgName = this.nativeView.getContext().getPackageName();
const id = this.nativeView.getContext().getResources().getIdentifier("search_src_text", "id", pkgName);
this._searchTextView = <android.widget.TextView>this.nativeView.findViewById(id);
}
return this._searchTextView;
}
private _getSearchPlate(): android.widget.LinearLayout {
const id = this.nativeView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
return <android.widget.LinearLayout>this.nativeView.findViewById(id);
if (!this._searchPlate) {
const pkgName = this.nativeView.getContext().getPackageName();
const id = this.nativeView.getContext().getResources().getIdentifier("search_plate", "id", pkgName);
this._searchPlate = <android.widget.LinearLayout>this.nativeView.findViewById(id);
}
return this._searchPlate;
}
}