我的应用程序中有一个带有 SearchView 小部件的工具栏,有两个图标用于搜索图标,另一个只是一个简单的网格可绘制。当我点击搜索按钮时,我的工具栏会更改为SearchView,但是当我再次触摸我的工具栏的网格按钮时, SearchView 会变得可见但是我不想打开单击网格按钮上的 SearchView 。我只希望它对我的搜索按钮可见。
代码:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.vid_search_menu_items, menu);
//Add Tint to icon
Drawable drawable = menu.findItem(R.id.action_grid).getIcon();
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, ContextCompat.getColor(this,R.color.white));
menu.findItem(R.id.action_grid).setIcon(drawable);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
// Toast.makeText(getActivity(),"collapsed",Toast.LENGTH_LONG).show();
searchMeta.setVisibility(View.GONE);
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
// Toast.makeText(getActivity(),"expanded",Toast.LENGTH_LONG).show();
searchMeta.setVisibility(View.VISIBLE);
return true; // Return true to expand action view
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_search:
Toast.makeText(this,"search",Toast.LENGTH_LONG).show();
break;
case R.id.action_grid:
Toast.makeText(this,"search",Toast.LENGTH_LONG).show();
isList = !isList;
if(isList){
//Change Item Icon
Drawable myDrawable = ContextCompat.getDrawable(this, R.drawable.ic_list_2);
myDrawable = DrawableCompat.wrap(myDrawable);
DrawableCompat.setTint(myDrawable, ContextCompat.getColor(this,R.color.white));
item.setIcon(myDrawable);
}else{
Drawable myDrawable = ContextCompat.getDrawable(this, R.drawable.ic_grid_2);
myDrawable = DrawableCompat.wrap(myDrawable);
DrawableCompat.setTint(myDrawable, ContextCompat.getColor(this,R.color.white));
item.setIcon(myDrawable);
}
break;
}
工具栏布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_frag"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.AppBarOverlay"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tool_bar_icon"
android:layout_gravity="center_horizontal"
android:text="Videos"
android:id="@+id/page_title"
android:textStyle="bold"
/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:visibility="gone"
android:id="@+id/search_meta"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/brands"
android:layout_marginEnd="5dp"
android:popupBackground="#fff"
android:theme="@style/CustomSpinnerStyle"
/>
<Spinner
android:theme="@style/CustomSpinnerStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/categories"
android:layout_marginStart="5dp"
android:popupBackground="#fff"
/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
项目文件:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:tint="@color/white"
/>
<item
android:id="@+id/action_grid"
android:icon="@drawable/ic_grid_2"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:tint="@color/white"
/>
</menu>
答案 0 :(得分:0)
从第二项中移除app:actionViewClass="android.support.v7.widget.SearchView"
并看到魔法:)