NavigationDrawer menuItem setChecked 不起作用
NavigationDrawer menuItem setChecked doesnt work
我使用 Android 支持设计库中的 NavigationDrawer 创建了一个 BaseActivity。
public abstract class BaseActivity extends ActionBarActivity {
final static int MENUITEMS = 5;
private Toolbar toolbar; // Declaring the Toolbar Object
NavigationView navigationView;
private DrawerLayout drawerLayout;
Context basecontext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
/* Assinging the toolbar object ot the view
and setting the the Action bar to our toolbar
*/
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.menu_start:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BaseActivity.this, Start.class);
startActivity(intent);
// For rest of the options we just show a toast on click
//TODO
case R.id.menu_all_entries:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
case R.id.menu_missed_entries:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
case R.id.menu_diagram:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
case R.id.menu_export:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
default:
Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
}
}
//Closing drawer on item click
drawerLayout.closeDrawers();
//String localClassName = getLocalClassName();
//Check to see which item was being clicked and perform appropriate action
return true;
}
});
这很好用。
现在我有一个 "Sub"-Activity(没有表示为菜单项的内容)。
在这个Activity中:
public class AddEntryPage1 extends BaseActivity {
@Override
protected int getLayoutId(){
return R.layout.activity_add_entry_page1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
for(int i=0;i<MENUITEMS;i++){
navigationView.getMenu().getItem(0).setChecked(false);
}
}
}
我试图将所有菜单项设置为未选中,但没有任何反应。
当我计算表达式时
navigationView.getMenu().getItem(0).isChecked()
它总是正确的(在
for-loop/getItem(0).setChecked(false))
.
我也没有收到任何错误。
好像我没有写权限?
我用选定的项目启动 drawer.xml 是否有问题?当我单击另一个菜单项时,所选项目会按预期更改。
还是 checkableBehavior="single" 意味着必须检查一项?
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_start"
android:checked="true"
android:icon="@drawable/ic_action_home"
android:title="Start" />
<item
android:id="@+id/menu_all_entries"
android:checked="false"
android:icon="@drawable/ic_action_view_list"
android:title="Alle Einträge" />
<item
android:id="@+id/menu_missed_entries"
android:checked="false"
android:icon="@drawable/ic_action_view_list"
android:title="Verpasste Einträge" />
<item
android:id="@+id/menu_diagram"
android:checked="false"
android:icon="@drawable/ic_editor_insert_chart"
android:title="Diagramm" />
<item
android:id="@+id/menu_export"
android:checked="false"
android:icon="@drawable/ic_social_share"
android:title="Export" />
</group>
谢谢你帮我!
//The click listner for ListView in the navigation drawer
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
}
}
怎么样:
尝试删除:
android:checked="false"
如果有帮助,我的代码是:
navigationView = binding.navigationView;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawerLayout.closeDrawers();
int itemId = menuItem.getItemId();
navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
return true;
}
});
找到原因了:
奇怪
navigationView.getMenu().getItem(1).setChecked(false);
activates/checks条目
和
.setChecked(true)
也activates/checks吧。
任何人都可以复制这个吗?这可能是一个错误吗?
对于可能遇到相同问题的任何人:
我向 Google 报告了这个问题:
https://code.google.com/p/android/issues/detail?id=184089
我使用 Android 支持设计库中的 NavigationDrawer 创建了一个 BaseActivity。
public abstract class BaseActivity extends ActionBarActivity {
final static int MENUITEMS = 5;
private Toolbar toolbar; // Declaring the Toolbar Object
NavigationView navigationView;
private DrawerLayout drawerLayout;
Context basecontext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
/* Assinging the toolbar object ot the view
and setting the the Action bar to our toolbar
*/
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.menu_start:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BaseActivity.this, Start.class);
startActivity(intent);
// For rest of the options we just show a toast on click
//TODO
case R.id.menu_all_entries:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
case R.id.menu_missed_entries:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
case R.id.menu_diagram:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
case R.id.menu_export:
Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
default:
Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
}
}
//Closing drawer on item click
drawerLayout.closeDrawers();
//String localClassName = getLocalClassName();
//Check to see which item was being clicked and perform appropriate action
return true;
}
});
这很好用。
现在我有一个 "Sub"-Activity(没有表示为菜单项的内容)。
在这个Activity中:
public class AddEntryPage1 extends BaseActivity {
@Override
protected int getLayoutId(){
return R.layout.activity_add_entry_page1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
for(int i=0;i<MENUITEMS;i++){
navigationView.getMenu().getItem(0).setChecked(false);
}
}
}
我试图将所有菜单项设置为未选中,但没有任何反应。
当我计算表达式时
navigationView.getMenu().getItem(0).isChecked()
它总是正确的(在 for-loop/getItem(0).setChecked(false)) .
我也没有收到任何错误。
好像我没有写权限? 我用选定的项目启动 drawer.xml 是否有问题?当我单击另一个菜单项时,所选项目会按预期更改。 还是 checkableBehavior="single" 意味着必须检查一项?
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_start"
android:checked="true"
android:icon="@drawable/ic_action_home"
android:title="Start" />
<item
android:id="@+id/menu_all_entries"
android:checked="false"
android:icon="@drawable/ic_action_view_list"
android:title="Alle Einträge" />
<item
android:id="@+id/menu_missed_entries"
android:checked="false"
android:icon="@drawable/ic_action_view_list"
android:title="Verpasste Einträge" />
<item
android:id="@+id/menu_diagram"
android:checked="false"
android:icon="@drawable/ic_editor_insert_chart"
android:title="Diagramm" />
<item
android:id="@+id/menu_export"
android:checked="false"
android:icon="@drawable/ic_social_share"
android:title="Export" />
</group>
谢谢你帮我!
//The click listner for ListView in the navigation drawer
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
}
}
怎么样:
尝试删除:
android:checked="false"
如果有帮助,我的代码是:
navigationView = binding.navigationView;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawerLayout.closeDrawers();
int itemId = menuItem.getItemId();
navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
return true;
}
});
找到原因了:
奇怪
navigationView.getMenu().getItem(1).setChecked(false);
activates/checks条目
和
.setChecked(true)
也activates/checks吧。
任何人都可以复制这个吗?这可能是一个错误吗?
对于可能遇到相同问题的任何人:
我向 Google 报告了这个问题: https://code.google.com/p/android/issues/detail?id=184089