gpt4 book ai didi

Android:多次检查的菜单项问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:06 25 4
gpt4 key购买 nike

这是我的应用程序的行为(当然只有 1 是正确的):

enter image description here

当然我现在只想检查一个项目。

我将项目分为两组(要添加分隔线,请参阅我之前的问题:How add horizontal separator in navdrawer?)

这是 nav_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single"
android:id="@+id/group1" >

<item
android:id="@+id/home"
android:checked="false"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/list_home" />

<item
android:id="@+id/list_event"
android:checked="false"
android:icon="@drawable/ic_list_black_24dp"
android:title="@string/list_event" />

</group>

<group
android:checkableBehavior="single"
android:id="@+id/group2" >

<item
android:id="@+id/settings"
android:checked="false"
android:icon="@drawable/ic_settings_black_24dp"
android:title="@string/settings" />

</group>


</menu>

这是管理 NavDrawer 的 BaseApp:

package com.xx.views;

import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarDrawerToggle;
import android.os.Bundle;
import android.view.View;

import com.xx.R;
import com.xx.mappers.DateManager;

public class BaseApp extends AppCompatActivity {

//Defining Variables
protected String LOGTAG = "LOGDEBUG";
protected Toolbar toolbar;
protected NavigationView navigationView;
protected DrawerLayout drawerLayout;

private DateManager db = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout);

navigationView = (NavigationView) findViewById(R.id.navigation_view);

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, new DashboardFragment());
fragmentTransaction.commit();

setNavDrawer();

// make home as checked
navigationView.getMenu().getItem(0).setChecked(true);
}

private void setNavDrawer(){

// Initializing Toolbar and setting it as the actionbar
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);

//Closing drawer on item click
drawerLayout.closeDrawers();


//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {

case R.id.home:

DashboardFragment dashboardFragment = new DashboardFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
fragmentTransaction.commit();
return true;

case R.id.list_event:
ListEventFragment fragmentListEvent = new ListEventFragment();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragmentListEvent);
fragmentTransaction.commit();
return true;

case R.id.settings:
SettingsFragment fragmentSettings = new SettingsFragment();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragmentSettings);
fragmentTransaction.commit();
return true;

default:

return true;

}
}
});

// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle =
new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open_drawer, R.string.close_drawer){

@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything
// to happen so we leave this blank
super.onDrawerClosed(drawerView);
}

@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything
// to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};

//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);

//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}

private void eraseTable(){
db=new DateManager(this);
db.resetTable();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

非常感谢

最佳答案

这可以帮助你:尝试从 xml 中删除标签 android:checkableBehavior="single" 并为每个项目单独设置 android:checkable = “true”,然后在 Activity 中和 onNavigationItemSelected 事件中声明一个 MenuItem 对象,如果先前声明的 MenuItem 对象不为空,则将选中的值设置为 false,然后将作为参数接收的当前选定 menuItem 保存到先前声明的 MenuItem 对象。

这将在偶数子项上设置选中选择。

if (prevMenuItem != null) {
prevMenuItem.setChecked(false);
}

menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
prevMenuItem = menuItem;
return true;

我找到了这个解决方案 here

关于Android:多次检查的菜单项问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31254626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com