Show/hide 使用 onOptionsItemSelected 的搜索视图
Show/hide Searchview using onOptionsItemSelected
我想让我的 Searchview 仅在我从抽屉中打开产品选项时可见,为此我正在尝试使用 onOptionsItemSelected 方法来监听正在使用的选项。
问题是每次我尝试从 onOptionsItemSelected 获取我的 SearchView id 时,我总是得到一个 NullPointerException。
我知道如何使用以下方法在 onCreateOptionsMenu 中隐藏 SearchView:
menu.findItem(R.id.action_search).setVisible(false);
这是我的 onCreateOptionsMenu 的源代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
//id in this line is found
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
return true;
但我不能在 onOptionsItemSelected 方法中做同样的事情,因为它使用的参数是 MenuItem 而不是 Menu。
这是我对 onOptionsItemSelected 方法的最新尝试:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
//id in this line is null
MenuItem item_search = menu.findItem(R.id.action_search);
Log.d(TAG, "Response : " + item_search);
SearchView searchView = (SearchView) item_search.getActionView();
if (item.getItemId() == R.id.nav_produk) {
searchView.setVisibility(View.VISIBLE);
return true;
} else {
Log.d(TAG, "Response : " + searchView);
searchView.setVisibility(View.INVISIBLE);
return super.onOptionsItemSelected(item);
}
}
这是我的 res/menu/main.xml 文件:
<?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_settings"
android:visible="false"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:visible="true"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
我已经找到答案了!显然我根本不需要 onOptionsItemSelected。
我在 DrawerLayout 上使用 NavController,因此创建监听器的更简单方法是在 onCreateOptionsMenu[=11 中使用 onDestinationChanged 方法=]
这是结果:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
//Only show SearchView when produk is open
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_produk) {
menu.findItem(R.id.action_search).setVisible(true);
} else {
menu.findItem(R.id.action_search).setVisible(false);
}
}
});
return true;
}
我想让我的 Searchview 仅在我从抽屉中打开产品选项时可见,为此我正在尝试使用 onOptionsItemSelected 方法来监听正在使用的选项。 问题是每次我尝试从 onOptionsItemSelected 获取我的 SearchView id 时,我总是得到一个 NullPointerException。
我知道如何使用以下方法在 onCreateOptionsMenu 中隐藏 SearchView:
menu.findItem(R.id.action_search).setVisible(false);
这是我的 onCreateOptionsMenu 的源代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
//id in this line is found
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
return true;
但我不能在 onOptionsItemSelected 方法中做同样的事情,因为它使用的参数是 MenuItem 而不是 Menu。
这是我对 onOptionsItemSelected 方法的最新尝试:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
//id in this line is null
MenuItem item_search = menu.findItem(R.id.action_search);
Log.d(TAG, "Response : " + item_search);
SearchView searchView = (SearchView) item_search.getActionView();
if (item.getItemId() == R.id.nav_produk) {
searchView.setVisibility(View.VISIBLE);
return true;
} else {
Log.d(TAG, "Response : " + searchView);
searchView.setVisibility(View.INVISIBLE);
return super.onOptionsItemSelected(item);
}
}
这是我的 res/menu/main.xml 文件:
<?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_settings"
android:visible="false"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:visible="true"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
我已经找到答案了!显然我根本不需要 onOptionsItemSelected。
我在 DrawerLayout 上使用 NavController,因此创建监听器的更简单方法是在 onCreateOptionsMenu[=11 中使用 onDestinationChanged 方法=]
这是结果:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
//Only show SearchView when produk is open
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_produk) {
menu.findItem(R.id.action_search).setVisible(true);
} else {
menu.findItem(R.id.action_search).setVisible(false);
}
}
});
return true;
}