当前位置:   article > 正文

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

fragment 调用 navigationview


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

    }
}


2.可以自定义NavigationView

  1. <android.support.design.widget.NavigationView
  2. android:id="@+id/navigation"
  3. android:layout_width="wrap_content"
  4. android:layout_height="match_parent"
  5. android:layout_gravity="start"
  6. android:fitsSystemWindows="true">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical">
  11. <include layout="@layout/nav_header" />
  12. <ListView
  13. android:id="@+id/lst_menu_items"
  14. android:layout_width="match_parent"
  15. android:layout_height="0dp"
  16. android:layout_weight="1" />
  17. </LinearLayout>
  18. </android.support.design.widget.NavigationView>
3.鸿洋写了一个高仿产品

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/219209
推荐阅读
相关标签
  

闽ICP备14008679号