Viewpager+fragment+bottom-navigation-bar搭建底部导航框架

1.布局文件:




    

    


顶部的viewpager+底部的bottomnavgationbar

2.框架的配置:
2.1 初始化 viewpager

viewpager bottomNavigationBar = findViewById(R.id.bottom_navigation_bar);

2.2 初始化fragment

    private void initFragment() {
        mHomefragment =new HomeFragmnent();
        mNavgationfragment = new NavgationFragment();
        mSystemfragment = new SystemFragment();
        mSubjectfragment = new SubjectFragment();
        mMefragment = new MeFragment();
        mFragmentList.add(mHomefragment);
        mFragmentList.add(mNavgationfragment);
        mFragmentList.add(mSystemfragment);
        mFragmentList.add(mSubjectfragment);
        mFragmentList.add(mMefragment);
    }

2.3
初始化底部条目

    private void initBottomItems() {
        BottomNavigationItem homeItem = new BottomNavigationItem(R.drawable.select_home, "首页").
                setActiveColorResource(R.color.common_blue)
                .setInActiveColorResource(R.color.text_normal);
        BottomNavigationItem knowItem = new BottomNavigationItem(R.drawable.select_knowledge, "知识").
                setActiveColorResource(R.color.common_blue)
                .setInActiveColorResource(R.color.text_normal);
        BottomNavigationItem navItem = new BottomNavigationItem(R.drawable.select_navgation, "体系").
                setActiveColorResource(R.color.common_blue)
                .setInActiveColorResource(R.color.text_normal);
        BottomNavigationItem projectItem = new BottomNavigationItem(R.drawable.select_project, "项目").
                setActiveColorResource(R.color.common_blue)
                .setInActiveColorResource(R.color.text_normal);
        BottomNavigationItem meItem = new BottomNavigationItem(R.drawable.select_ne, "我的").
                setActiveColorResource(R.color.common_blue)
                .setInActiveColorResource(R.color.text_normal);

                 bottomNavigationBar.addItem(homeItem).addItem(knowItem).addItem(navItem).addItem(projectItem).addItem(meItem).
                setMode(BottomNavigationBar.MODE_SHIFTING)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)
                .setBarBackgroundColor(R.color.common_white)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
mViewpwgwe.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

    }

2.4 设置viewpager的适配器,将viewpager和fragment绑定,同时处理pager滑动后的底部导航的回复

    private void initViewpager() {
        MainFragmentPagerAdapter adapter = new MainFragmentPagerAdapter(getSupportFragmentManager(),mFragmentList);
        mViewpwgwe.setAdapter(adapter);
        mViewpwgwe.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationBar.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

2.5 MainFragmentPagerAdapter 适配器

public class MainFragmentPagerAdapter extends FragmentPagerAdapter {
    private List mFragmentList;

    public MainFragmentPagerAdapter(FragmentManager fm,List list) {
        super(fm);
        this.mFragmentList = list;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

}

你可能感兴趣的:(Viewpager+fragment+bottom-navigation-bar搭建底部导航框架)