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


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

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

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

关于NavigationView的使用:

1.使用xml配置headerLayout 和menu

    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"?>
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">

            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=""/>

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

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


menu

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


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

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

            
            

       

点击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(); // 隐藏当前的fragmentadd下一个到Activity        } else {
            ft.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个  
        }

    }
}


2.可以自定义NavigationView


    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

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

         layout="@layout/nav_header" />

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

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

你可能感兴趣的:(Android,Basic,Technology)