NavigationView配合Fragment切换+动画+使用+check问题解决方案

令狐新翰
2023-12-01


注意在使用Fragment切换动画时要注意fragment是不是v4包

因为v4包跟以后的版本的动画不同具体看:

http://blog.csdn.net/lvwenbo0107/article/details/50887861

关于NavigationView的使用:

1.使用xml配置headerLayout 和menu

<android.support.design.widget.NavigationView
    android:id="@+id/id_navigationView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header_just_username"
    app:menu="@menu/menu_drawer"
    />
header

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="192dp"
                android:background="?attr/colorPrimaryDark"
                android:orientation="vertical"
                android:padding="16dp"
                android:fitsSystemWindows="true"
                android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <TextView
        android:id="@+id/id_link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="16dp"
        android:text=""/>

    <TextView
        android:id="@+id/id_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/id_link"
        android:text="菜单"/>

    <ImageView
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_above="@id/id_username"
        android:layout_marginBottom="16dp"
        android:src="@mipmap/ic_launcher"/>


</RelativeLayout>
menu

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


    <group  >
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_dashboard"
            android:title="常用功能">
            <menu>
                <item
                    android:id="@+id/id_detection"
                    android:icon="@drawable/ic_dashboard"
                    android:title="@string/detection"
                    android:checkable="true" />
                <item
                    android:id="@+id/id_calibration"
                    android:icon="@drawable/ic_event"
                    android:title="@string/calibration"
                    android:checkable="true"/>
                <item
                    android:id="@+id/id_manualcalibration"
                    android:icon="@drawable/ic_headset"
                    android:title="@string/manualcalibration"
                    android:checkable="true"/>
                <item
                    android:id="@+id/id_detectionrecord"
                    android:icon="@drawable/ic_forum"
                    android:title="@string/detectionrecord"
                    android:checkable="true"/>
                <item
                    android:id="@+id/id_calibrationrecord"
                    android:icon="@drawable/ic_headset"
                    android:title="@string/calibrationrecord"
                    android:checkable="true"/>
                <item
                    android:id="@+id/id_operatelog"
                    android:icon="@drawable/ic_forum"
                    android:title="@string/operatelog"
                    android:checkable="true"/>
                <item
                    android:id="@+id/id_substancedb"
                    android:icon="@drawable/ic_event"
                    android:title="@string/substancedb"
                    android:checkable="true"/>

            </menu>
            </item>
        <item
            android:id="@+id/nav_messages"
            android:icon="@drawable/ic_event"
            android:title="基本功能">
            <menu>
                <item
                    android:id="@+id/id_usermanager"
                    android:icon="@drawable/ic_forum"
                    android:title="@string/usermanager"
                    android:checkable="true"/>
                <item
                    android:id="@+id/id_setup"
                    android:icon="@drawable/ic_headset"
                    android:title="@string/setup"
                    android:checkable="true"/>

            </menu>
            </item>

       </group>

</menu>

点击navigationview时会发现menuItem的check总是出问题于是做了如下修改:

navigationView = (NavigationView) findViewById(R.id.id_navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        mMenuItemID = menuItem.getItemId();
        switch (mMenuItemID) {

            case R.id.id_detection:
                setTitle(R.string.detection);
                if(mainFragment==null)
                    mainFragment=new MainFragment();
                switchContent(mFragment,mainFragment);
                break;
            case R.id.id_calibration:
                setTitle(R.string.calibration);
                if(mainFragment==null)
                    mainFragment=new MainFragment();
                switchContent(mFragment, mainFragment);

                break;
            case R.id.id_manualcalibration:
                setTitle(R.string.manualcalibration);
                if(mainFragment==null)
                    mainFragment=new MainFragment();
                switchContent(mFragment,mainFragment);
                break;
            case R.id.id_calibrationrecord:
                setTitle(R.string.calibrationrecord);
                if(calibrationRecordFragment==null)
                    calibrationRecordFragment=new CalibrationRecordFragment();
                switchContent(mFragment, calibrationRecordFragment);
                break;
            case R.id.id_detectionrecord:
                setTitle(R.string.detectionrecord);
                if(detectionRecordFragment==null)
                    detectionRecordFragment=new DetectionRecordFragment();
                switchContent(mFragment,detectionRecordFragment);
                break;
            case R.id.id_operatelog:
                setTitle(R.string.operatelog);
                if(operatelogFragment==null)
                    operatelogFragment=new OperatelogFragment();
                switchContent(mFragment,operatelogFragment);
                break;
            case R.id.id_substancedb:
                setTitle(R.string.substancedb);
                if(substanceDBFragment==null)
                    substanceDBFragment=new SubstanceDBFragment();
                switchContent(mFragment,substanceDBFragment);
                break;
        }
//重点在这里
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.menu_drawer);
        navigationView.getMenu().findItem(mMenuItemID).setChecked(true);
        mDrawerLayout.closeDrawer(Gravity.LEFT);
        return true;

    }
});

public void switchContent(Fragment from, Fragment to) {
    if (mFragment != to) {
        mFragment = to;
        FragmentManager fm = getFragmentManager();
        //添加渐隐渐现的动画  
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations( R.animator.fragment_slide_left_enter,
                R.animator.fragment_slide_right_exit);
        if (!to.isAdded()) {    // 先判断是否被add过  
            ft.hide(from).add(R.id.id_fragmentMain, to).commit(); // 隐藏当前的fragment,add下一个到Activity中  
        } else {
            ft.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个  
        }

    }
}


2.可以自定义NavigationView

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/nav_header" />

        <ListView
            android:id="@+id/lst_menu_items"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</android.support.design.widget.NavigationView>
3.鸿洋写了一个高仿产品

http://blog.csdn.net/lmj623565791/article/details/46405409

 类似资料: