我應該聽取什麼事件來確定用戶何時點擊圖標化。我想從操作欄中刪除一些項目(ActionBar導航選項卡,如果這很重要),以縱向方式創建更多空間。當searchview從圖標化視圖展開時發生的事件
我試過OnClickListener,OnFocusChangeListener,OnTouchListener和其他事件,但都沒有通過SearchView擴展觸發。
我應該聽取什麼事件來確定用戶何時點擊圖標化。我想從操作欄中刪除一些項目(ActionBar導航選項卡,如果這很重要),以縱向方式創建更多空間。當searchview從圖標化視圖展開時發生的事件
我試過OnClickListener,OnFocusChangeListener,OnTouchListener和其他事件,但都沒有通過SearchView擴展觸發。
由於API等級14,你有一個專門的監聽器: http://developer.android.com/guide/topics/ui/actionbar.html
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
MenuItem menuItem = menu.findItem(R.id.actionItem);
...
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
我找到了一種方法來獲取事件中使用addOnLayoutChangeListener
private final OnLayoutChangeListener _searchExpandHandler = new OnLayoutChangeListener()
{
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom)
{
SearchView searchView = (SearchView)v;
if (searchView.isIconfiedByDefault() && !searchView.isIconified())
{
// search got expanded from icon to search box, hide tabs to make space
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
}
};
完美! (儘管你可能想用'v'而不是'_searchView'來傳遞)......我找不到這個方法,因爲這個方法和其他監聽器一樣,以'add'開始,而不是'set'。 –
需要API級別11,我想在級別8.使用幫助 – vuhung3990
也可以實現成searchView.addOnLayoutChangeListener(); – Maga
如果您使用MenuItemCompat
:
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Log.d("TAG", "Collapsed");
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Log.d("TAG", "Expanded");
return true;
}
});
與往常一樣,您可以使用** ActionBarSherlock **('com.actionbarsherlock.view.MenuItem')來實現向後兼容性 –
並且如果使用AppCompat,則有'MenuItemCompat.setOnActionExpandListener' –