我正在创建一个自定义 SearchView,但到目前为止,如果用户单击 searchView 小部件,我还没有找到(单击)事件或侦听器。所以 SearchView 关闭,用户点击搜索图标(哪个监听器?!),SearchView 被打开。见下图。我需要那个事件,因为我想在用户打开 searchView 后更改 UI
编辑: 到目前为止我尝试的是
setOnClickListener
到 SearchView 元素 -> 但只有在 SearchView 打开时才会触发(第二张图片)搜索
SearchView
了 Api,但没有该操作的侦听器(或者我是盲人?)尝试了
onWindowFocusChanged
searchView 的解决方法,但由于 searchView 隐藏在某种 youtube/facebook 侧边栏中,这不起作用
有任何想法吗?我也附上了我的 SearchView 代码,但不要认为代码是错误的。只是还没有听众。顺便说一句:欢迎任何解决方法。但是,如果 SearchView 一开始就关闭,那就太好了!(如上图所示)
public class AmplifySearchView extends SearchView implements
OnQueryTextListener, OnFocusChangeListener,
OnCloseListener {
private static final String DEBUG_TAG = "AmplifySeachView";
private Context context;
private DataBaseController datasource;
private StationListView searchResultListView;
private TextView noResultTextView;
private Boolean searchStarted = false;
public AmplifySearchView(Context context) {
super(context);
this.context = context;
}
public AmplifySearchView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void initSearchView(StationListView view, TextView noResultTextView) {
this.searchResultListView = view;
this.searchResultListView.setActivityId(Constants.UI_SEARCH_RESULT_LIST);
this.noResultTextView = noResultTextView;
this.setOnQueryTextListener(this);
this.setOnFocusChangeListener(this);
this.setOnCloseListener(this);
this.setOnCloseListener(this);
setIconifiedByDefault(true);
datasource = DataBaseController.getInstance(context);
int id = this.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) findViewById(id);
textView.setTextColor(Color.WHITE);
resetSearchInputTextField();
}
private void resetSearchInputTextField() {
setQueryHint(getResources().getString(R.string.search_hint));
}
@Override
public boolean onClose() {
Log.d(DEBUG_TAG, "onClose()");
searchResultListView.setVisibility(View.GONE);
noResultTextView.setVisibility(View.GONE);
searchStarted = false;
Intent intent = new Intent(Constants.SEARCH_ENDED);
intent.addCategory(Constants.UI_AMPLIFY_CATEGORY);
context.sendBroadcast(intent);
return false;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(DEBUG_TAG,"onFocusChange()" + hasFocus);
}
@Override
public boolean onQueryTextChange(String newText) {
//do sth...
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + query);
return true;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction,
Rect previouslyFocusedRect) {
Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + gainFocus);
// TODO Auto-generated method stub
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
}
@Override
public void onActionViewCollapsed() {
Log.d(DEBUG_TAG, "onActionViewCollapsed");
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
Log.d(DEBUG_TAG, "onActionViewExpanded");
super.onActionViewExpanded();
}
}
顺便说一句:焦点更改在这里不起作用,因为 searchview 在开始时是不可见的,然后在 ui 中插入......