DrawerLayout+FragmentTabHost+ToolBar实现左滑菜单及fragment切换

1、drawerlayout是谷歌官方的侧滑菜单布局,drawerlayout下的第一个布局被视为主布局,第二个被视为左滑菜单,第三个为右滑菜单

.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.AppBarOverlay" >
    .support.v7.widget.Toolbar>

.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        "match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            "@+id/fl_realcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
            "match_parent"
                android:layout_height="1dp"
                android:background="#eee"/>
            "match_parent"
                android:layout_height="wrap_content"
                android:background="#fff">
                .support.v4.app.FragmentTabHost
                    android:id="@+id/tab_host"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                "@+id/iv_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/btn_quickoption_nor"
                    android:layout_gravity="center"/>
            


        
        "match_parent"
            android:layout_height="match_parent"
            android:layout_gravity ="left"
            android:background="#eee"
            >
    .support.v4.widget.DrawerLayout>

2、在Java代码中通过ActionBarDrawerToggle关联toolbar和drawerlayout

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,
        mDrawerLayout, mToolbar, R.string.app_name, R.string.action_settings);
mActionBarDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

3、在主题样式文件中设置为NoActionBar,防止actionbar和toolbar冲突,style.xml文件如下


    

    -- 左边的箭头指示-->
    

4、fragmentTabhost实现fragment切换
(1) 新建一个Indicator枚举类封装页面底部的tab键

public enum Indicator {
    Main(R.drawable.tab_icon_new,"Fragment1", MainFragment.class),
    NEWS(R.drawable.tab_icon_tweet,"Fragment2", NewsFragment.class),
    QUICK(R.drawable.tab_icon_new,"Fragment3", MainFragment.class),
    FIND(R.drawable.tab_icon_explore,"Fragment4", FindFragment.class),
    ME(R.drawable.tab_icon_me,"Fragment5", MeFragment.class);
    int resIcon;
    String tabName;
    Class clz;
    Indicator(int resIcon,String tabName,Class clz){
        this.resIcon = resIcon;
        this.tabName = tabName;
        this.clz = clz;

    }

    public Class getClz() {
        return clz;
    }

    public void setClz(Class clz) {
        this.clz = clz;
    }

    public int getResIcon() {
        return resIcon;
    }

    public void setResIcon(int resIcon) {
        this.resIcon = resIcon;
    }

    public String getTabName() {
        return tabName;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }
}
private void initTabHost() {
        fragmentTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        fragmentTabHost.setup(MainActivity.this,getSupportFragmentManager(),R.id.fl_realcontent);
        Indicator []indicators = Indicator.values();
        for(int i=0;i
            TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(indicators[i].getTabName());
            View view = getIndicatorView(indicators[i]);
            //中间的按钮点击跳转到activity而不是fragment
            if(i == 2){
                view.setVisibility(View.INVISIBLE);
            }
            tabSpec.setIndicator(view);
            fragmentTabHost.addTab(tabSpec,indicators[i].getClz(),null);
        }
        //设置fragmenttabhost的间隙为0
        fragmentTabHost.getTabWidget().setShowDividers(0);
        ImageView ivAdd = (ImageView) findViewById(R.id.iv_add);
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"test",Toast.LENGTH_SHORT).show();
            }
        });
    }

你可能感兴趣的:(DrawerLayout+FragmentTabHost+ToolBar实现左滑菜单及fragment切换)