blob: 25ddcf8e822e357eb52c8841531ae0f6a031dbe9 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.databinding.adapters;
import android.annotation.TargetApi;
import android.databinding.BindingAdapter;
import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.widget.RatingBar;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SearchView.OnSuggestionListener;
@BindingMethods({
@BindingMethod(type = SearchView.class, attribute = "android:onQueryTextFocusChange", method = "setOnQueryTextFocusChangeListener"),
@BindingMethod(type = SearchView.class, attribute = "android:onSearchClick", method = "setOnSearchClickListener"),
@BindingMethod(type = SearchView.class, attribute = "android:onClose", method = "setOnCloseListener"),
})
public class SearchViewBindingAdapter {
@BindingAdapter("android:onQueryTextChange")
public static void setListener(SearchView view, OnQueryTextChange listener) {
setListener(view, null, listener);
}
@BindingAdapter("android:onQueryTextSubmit")
public static void setListener(SearchView view, OnQueryTextSubmit listener) {
setListener(view, listener, null);
}
@BindingAdapter({"android:onQueryTextSubmit", "android:onQueryTextChange"})
public static void setListener(SearchView view, final OnQueryTextSubmit submit,
final OnQueryTextChange change) {
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
if (submit == null && change == null){
view.setOnQueryTextListener(null);
} else {
view.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (submit != null) {
return submit.onQueryTextSubmit(query);
} else {
return false;
}
}
@Override
public boolean onQueryTextChange(String newText) {
if (change != null) {
return change.onQueryTextChange(newText);
} else {
return false;
}
}
});
}
}
}
@BindingAdapter("android:onSuggestionClick")
public static void setListener(SearchView view, OnSuggestionClick listener) {
setListener(view, null, listener);
}
@BindingAdapter("android:onSuggestionSelect")
public static void setListener(SearchView view, OnSuggestionSelect listener) {
setListener(view, listener, null);
}
@BindingAdapter({"android:onSuggestionSelect", "android:onSuggestionClick"})
public static void setListener(SearchView view, final OnSuggestionSelect submit,
final OnSuggestionClick change) {
if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
if (submit == null && change == null) {
view.setOnSuggestionListener(null);
} else {
view.setOnSuggestionListener(new OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
if (submit != null) {
return submit.onSuggestionSelect(position);
} else {
return false;
}
}
@Override
public boolean onSuggestionClick(int position) {
if (change != null) {
return change.onSuggestionClick(position);
} else {
return false;
}
}
});
}
}
}
@TargetApi(VERSION_CODES.HONEYCOMB)
public interface OnQueryTextSubmit {
boolean onQueryTextSubmit(String query);
}
@TargetApi(VERSION_CODES.HONEYCOMB)
public interface OnQueryTextChange {
boolean onQueryTextChange(String newText);
}
@TargetApi(VERSION_CODES.HONEYCOMB)
public interface OnSuggestionSelect {
boolean onSuggestionSelect(int position);
}
@TargetApi(VERSION_CODES.HONEYCOMB)
public interface OnSuggestionClick {
boolean onSuggestionClick(int position);
}
}