安卓:这是多个检查问题与菜单项 [英] Android: issue with menu item that are multiple checked

查看:198
本文介绍了安卓:这是多个检查问题与菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的应用程序的beahviour(仅 1 是正确的,当然):

This is the beahviour of my App (only 1 is right, of course):

我当然希望只有一个项目的时刻检查。

Of course I want only one item at the moment checked.

我分项两组(加分频器,看到我的previous问题:的如何在navdrawer添加水平分隔符?

I divided the items in two groups (to add the divider, see my previous question: How add horizontal separator in navdrawer? )

这是nav_menu.xml:

This is the 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:

This is the BaseApp that manage the NavDrawer:

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);
    }

}

非常感谢你。

推荐答案

这可以帮助你:尝试移除标签的android:checkableBehavior =单,从XML 并设置的android:可检查=真为每个项目单独,然后在活动和onNavigationItemSelected事件声明一个MenuItem对象,如果previously宣布MenuItem对象不是null然后设置检查数值为假的,然后保存当前选择菜单项收到的参数早些时候宣布MenuItem对象。

This could help you: Try to removing the tag android:checkableBehavior="single" from xml and set android:checkable = "true" for each item individually, then declare a MenuItem object in the activity and in onNavigationItemSelected event if previously declared MenuItem object is not null then set checked value as false for it and then save current selected menuItem received as parameter to earlier declared MenuItem object.

这将设置检查选择在偶数子项。

This will set checked selection on even subitems.

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

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

我发现这个解决方案这里

这篇关于安卓:这是多个检查问题与菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆