12. 多個Fragment切換、menu菜單

1.fragment切換

當我們使用fragmentManager通過事務beginTransation管理多個fragment對象的時候,就要合理的顯示和隱藏Fragment。
1.把所有需要管理的Fragment加入到事務中。同時顯示需要的Fragment,隱藏其他的。 同時記錄當前的Fragmet引用。便於切換到其他fragment,使用hide方法,隱藏它。

  		zhihuFragment = new ZhihuFragment();
        wxFragment = new WxFragment();
        gankFragment = new GankFragment();

        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

        fragmentTransaction
                .add(R.id.content, zhihuFragment)
                .add(R.id.content, wxFragment)
                .add(R.id.content, gankFragment)
                .show(zhihuFragment)
                .hide(wxFragment)
                .hide(gankFragment)
                .commit();
        lastFragment = zhihuFragment;

2.切換fragment

 navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {

                    case R.id.zhihu:
                        if (!menuItem.isChecked()){
                            menuItem.setChecked(true);
                            type = ZHIHU_TYPE;
                            switchFragment();
                        }
                        break;
                    case R.id.wx:
                        type = WEIXIN_TYPE;
                        switchFragment();
                        break;
                    case R.id.gank:
                        type = GANK_TYPE;
                        switchFragment();
                        break;
              
                }
                drawerLayout.closeDrawer(Gravity.LEFT);
                return false;
            }
        });





    public void switchFragment() {
        Fragment currFragment = getCurrFragment();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.show(currFragment)
                .hide(lastFragment)
                .commit();
         // 切換完畢後,記錄當前fragmetn,便於下次隱藏該fragment使用。
        lastFragment = currFragment;

    }

   /**
     * 獲取當前 點擊的fragment 對象
     * @return
     */
    private Fragment getCurrFragment() {
        switch (type) {
            case ZHIHU_TYPE:
                if (zhihuFragment == null) {
                    zhihuFragment = new ZhihuFragment();
                }
                return zhihuFragment;

            case WEIXIN_TYPE:
                if (wxFragment == null) {
                    wxFragment = new WxFragment();
                }
                return wxFragment;
            case GANK_TYPE:
                if (gankFragment == null) {
                    gankFragment = new GankFragment();
                }
                return gankFragment;
    
        }

        return null;
    }

2.側滑菜單–分類標題

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

    <item android:title="資訊">

        <menu>

            <item
                android:id="@+id/zhihu"
                android:icon="@mipmap/ic_drawer_zhihu"
                android:title="知乎日報" />
            <item
                android:id="@+id/wx"
                android:icon="@mipmap/ic_drawer_wechat"
                android:title="微信精選" />
            <item
                android:id="@+id/gank"
                android:icon="@mipmap/ic_drawer_gank"
                android:title="乾貨集中營" />
            <item
                android:id="@+id/gold"
                android:icon="@mipmap/ic_drawer_gold"
                android:title="稀土掘金" />
            <item
                android:id="@+id/vtex"
                android:icon="@mipmap/ic_drawer_vtex"
                android:title="V2EX" />

        </menu>
    </item>
</menu>

在這裏插入圖片描述

3.側滑菜單–選中效果

       //如果有當前選擇過的菜單,點擊其他菜單
                //重置以前的選中狀態
                if (lastMenuItem!=null){
                    lastMenuItem.setChecked(false);
                    lastMenuItem.setCheckable(false);
                }
                //設置當前菜單的選中狀態
                menuItem.setChecked(true);
                menuItem.setCheckable(true);

                //  記錄當前選中的菜單
                lastMenuItem = menuItem;

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章