我正在使用ActionBarSherlock:
有3个菜单项,其中只有1个具有ActionLayout.
default_menu.xml
<item android:id="@+id/searchIcon" android:icon="@drawable/search_icon" android:actionLayout="@layout/search_icon_actionview" android:showAsAction="always|collapseActionView" android:title="Search"/> <item android:id="@+id/newIcon" android:icon="@drawable/new_icon" android:orderInCategory="0" android:showAsAction="ifRoom|collapseActionView" android:title="New"/> <item android:id="@+id/notificationIcon" android:icon="@drawable/notification_icon" android:orderInCategory="0" android:showAsAction="ifRoom|collapseActionView" android:title="Notifications"/>
如果我使用上面的代码.只有前2个图标可见.如果我将“第二”和“第三”视为“始终”,则即使在单击“搜索”时打开了actionLayout,它们也会保留.
我想最初显示所有3个菜单项,当点击search_icon时隐藏每个项目.
我对我的实施也有疑问,不管我是以正确的方式做事.
在Activity的(有一个viewPager)第一个片段:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.searchIcon: mEtSearchbar.clearFocus(); (new Handler()).postDelayed(new Runnable() { public void run() { mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,0)); mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,0)); } },100); return true; case R.id.newIcon: return true; case R.id.notificationIcon: return true; default: return super.onOptionsItemSelected(item); } } @Override public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) { // TODO Auto-generated method stub inflater.inflate(R.menu.default_menu,menu); mSearchbar = (MenuItem) menu.findItem(R.id.searchIcon); View actionview = mSearchbar.getActionView(); mEtSearchbar = ((EditText) actionview.findViewById(R.id.search_editText)); ImageView searchImage = ((ImageView) actionview.findViewById(R.id.search_image)); searchImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String s = mEtSearchbar.getText().toString(); Toast.makeText(getSherlockActivity(),"Searching for: " + s,Toast.LENGTH_SHORT) .show(); } }); super.onCreateOptionsMenu(menu,inflater); }
解决方法
这就是我最终使用的.
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.main,menu); MenuItem searchItem = menu.findItem(R.id.search); mSearchView = (SearchView) searchItem.getActionView(); setupSearchView(searchItem); mSearchView.setOnSearchClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // hide action item if (menu != null) { menu.findItem(R.id.notifications).setVisible(false); menu.findItem(R.id.share).setVisible(false); } } }); mSearchView.setOnCloseListener(new SearchView.OnCloseListener() { @Override public boolean onClose() { adapter.getFilter().filter(""); // re-show the action button if (menu != null) { menu.findItem(R.id.notifications).setVisible(true); menu.findItem(R.id.share).setVisible(true); } return false; } }); return true; } private void setupSearchView(MenuItem searchItem) { //code }