【问题标题】:Android: issue with menu item that are multiple checkedAndroid:多次选中的菜单项问题
【发布时间】:2015-07-06 20:03:11
【问题描述】:

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

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

我将项目分为两组(要添加分隔符,请参阅我之前的问题: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);
    }

}

非常感谢

【问题讨论】:

标签: android


【解决方案1】:

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

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

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

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

我找到了这个解决方案here

【讨论】:

    【解决方案2】:

    如果您使用groupsandroid:checkableBehavior="single",那么您只需将单个项目设置为导航视图中的选中项目(而不仅仅是使用item.setChecked(true) 选中的项目):

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
    
        int id = item.getItemId();
    
        //item.setChecked(true); //Won't work, will leave previous item checked too.
        navigationView.setCheckedItem(id); //this will check single item
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    

    现在,无论您选择还是模拟选择一个项目,它一次只会检查一个项目。

    【讨论】:

      【解决方案3】:

      将两个组放在一个组中

      android:checkableBehavior="single"
      

      创建父组

      【讨论】:

      • 工作和快速实施
      【解决方案4】:

      您必须实现自定义适配器、自定义/模型、自定义函数才能取消选中单击时的其他项目。

      我建议你使用this library。它是一个材质导航抽屉实现,它已经实现了所有这些逻辑。如果你不想使用一个库,你应该检查它是如何在这个库代码上完成的并适应你的需求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-21
        • 2021-02-16
        • 1970-01-01
        相关资源
        最近更新 更多