在Navigation Drawer中实现多个组的多个选择

时间:2016-10-29 12:44:28

标签: java android navigation-drawer androiddesignsupport

我希望在Navigation Drawer中进行此类选择。此图显示了最初进行的选择(默认情况下不是用户)。

enter image description here

我已通过以下代码

实现了这一点
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Category">
    <menu>
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/coaching"
                android:icon="@drawable/coaching"
                android:title="Coaching"
                android:checked="true"/>
            <item
                android:id="@+id/training"
                android:icon="@drawable/training"
                android:title="Training"/>
        </group>
    </menu>
</item>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/new_registrations"
        android:icon="@drawable/new_registrations"
        android:title="New Registrations"
        android:checked="true"/>
    <item
        android:id="@+id/ready_certificates"
        android:icon="@drawable/ready_certificates"
        android:title="Certificates Ready To Collect"/>
    <item
        android:id="@+id/allotted_certificates"
        android:icon="@drawable/allotted_certificates"
        android:title="Certificates Allotted So Far"/>
</group>

但事实是,当我手动选择任何项目时,将取消选择来自两个组的所有先前选择的项目。所以我想验证第一组中的一个选择和第二组中的一个选择。寻找缺失的属性。

1 个答案:

答案 0 :(得分:3)

  • 从两个组中删除android:checkableBehavior="single"
  • 从“coaching”和“new_registrations”项目中删除android:checked="true"
  • 分别为每个项目设置android:checkable="true"
  • 为两个组("@+id/first_group""@+id/second_group")设置唯一ID。

您的 activity_main_drawer.xml 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Category">
        <menu>
            <group android:id="@+id/first_group" >
                <item
                    android:id="@+id/coaching"
                    android:icon="@drawable/coaching"
                    android:title="Coaching"
                    android:checkable="true" />
                <item
                    android:id="@+id/training"
                    android:icon="@drawable/training"
                    android:title="Training"
                    android:checkable="true" />
            </group>
        </menu>
    </item>
    <group android:id="@+id/second_group" >
        <item
            android:id="@+id/new_registrations"
            android:icon="@drawable/new_registrations"
            android:title="New Registrations"
            android:checkable="true" />
        <item
            android:id="@+id/ready_certificates"
            android:icon="@drawable/ready_certificates"
            android:title="Certificates Ready To Collect"
            android:checkable="true" />
        <item
            android:id="@+id/allotted_certificates"
            android:icon="@drawable/allotted_certificates"
            android:title="Certificates Allotted So Far"
            android:checkable="true" />
    </group>
</menu>
  • MainActivity.java 中声明两个MenuItem个对象并对其进行检查。
  • 将以下逻辑添加到onNavigationItemSelected

您的 MainActivity.java 应如下所示:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    MenuItem prevItemOfFirstGroup;
    MenuItem prevItemOfSecondGroup;

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

        // ... some code

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        prevItemOfFirstGroup=navigationView.getMenu().findItem(R.id.coaching);
        prevItemOfFirstGroup.setChecked(true);
        prevItemOfSecondGroup=navigationView.getMenu().findItem(R.id.new_registrations);
        prevItemOfSecondGroup.setChecked(true);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int groupId = item.getGroupId();
        if (groupId == R.id.first_group) {
            if (prevItemOfFirstGroup != null) {
                prevItemOfFirstGroup.setChecked(false);
            }
            prevItemOfFirstGroup = item;
        } else if (groupId == R.id.second_group) {
            if (prevItemOfSecondGroup != null) {
                prevItemOfSecondGroup.setChecked(false);
            }
            prevItemOfSecondGroup = item;
        }
        item.setChecked(true);

        // Handle navigation view item clicks here.
        int id = item.getItemId();
        // ... some code

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return false; // IMPORTANT! NOT TRUE!
    }
}