使用Viewpager和TabLayout实现BottomNavigationView

时间:2019-07-11 23:55:46

标签: android android-viewpager android-tablayout bottomnavigationview

我是android开发的新手,我尝试实现一个接口合并 tabLayout,viewpager和bottomNavigationView和。我结束了一些 “有效”的代码,但我觉得这不是最好的方法。

 @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
        bottomNav = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        mainViewPager = (ViewPager) findViewById(R.id.main_tabs_pager);
        myTabsAccesorAdapter = new TabsAccesorAdapter(getSupportFragmentManager());
        mainViewPager.setAdapter(myTabsAccesorAdapter);

        mainTableLayout = (TabLayout) findViewById(R.id.main_tabs);
        mainTableLayout.setupWithViewPager(mainViewPager);

    }


 private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    Fragment fragment = null;
                    switch ( item.getItemId())
                    {
                        case R.id.ic_house:

                            notifsTableLayout.setVisibility(View.GONE);
                            notifsViewPager1.setVisibility(View.GONE);

                            searchViewPager.setVisibility(View.GONE);
                            savedViewPager.setVisibility(View.GONE);
                            savedTableLayout.setVisibility(View.GONE);
                            mainTableLayout.setVisibility(View.VISIBLE);
                            mainViewPager.setVisibility(View.VISIBLE);
                            appBarLayout.setVisibility(View.VISIBLE);

                            myTabsAccesorAdapter = new TabsAccesorAdapter(getSupportFragmentManager());
                            mainViewPager.setAdapter(myTabsAccesorAdapter);
                            mainTableLayout.setupWithViewPager(mainViewPager);
                            CURRENT_STATE = false;

                            break;

                        case R.id.ic_search:
                            mainTableLayout.setVisibility(View.GONE);
                            mainViewPager.setVisibility(View.GONE);
                            notifsTableLayout.setVisibility(View.GONE);
                            notifsViewPager1.setVisibility(View.GONE);
                            savedViewPager.setVisibility(View.GONE);
                            savedTableLayout.setVisibility(View.GONE);
                            appBarLayout.setVisibility(View.VISIBLE);


                            searchViewPager.setVisibility(View.VISIBLE);

                            mainViewPager.removeView(mainTableLayout);
                            fragment = new SearchFragment();
                            getSupportFragmentManager().beginTransaction().replace(R.id.main_tabs_pager_search, fragment).commit();


                            break;



                        case R.id.ic_look:
                            appBarLayout.setVisibility(View.VISIBLE);

                            break;

                        case R.id.ic_saved:

                            appBarLayout.setVisibility(View.VISIBLE);

                            mainTableLayout.setVisibility(View.GONE);
                            mainViewPager.setVisibility(View.GONE);
                            notifsTableLayout.setVisibility(View.GONE);
                            notifsViewPager1.setVisibility(View.GONE);

                            searchViewPager.setVisibility(View.GONE);
                            savedViewPager.setVisibility(View.VISIBLE);
                            savedTableLayout.setVisibility(View.VISIBLE);


                            savedFragmentAdaptor = new SavedFragmentAdaptor(getSupportFragmentManager());
                            savedViewPager.setAdapter(savedFragmentAdaptor);
                            savedTableLayout.setupWithViewPager(savedViewPager);

                            break;

                        case R.id.ic_messages:

                            appBarLayout.setVisibility(View.VISIBLE);

                            savedViewPager.setVisibility(View.GONE);
                            savedTableLayout.setVisibility(View.GONE);
                            searchViewPager.setVisibility(View.GONE);
                            mainTableLayout.setVisibility(View.GONE);
                            mainViewPager.setVisibility(View.GONE);
                            notifsViewPager1.setVisibility(View.VISIBLE);
                            notifsTableLayout.setVisibility(View.VISIBLE);

                            tabsAccesorNotificationsAdapter = new TabsAccesorNotificationsAdapter(getSupportFragmentManager());
                            notifsViewPager1.setAdapter(tabsAccesorNotificationsAdapter);
                            notifsTableLayout.setupWithViewPager(notifsViewPager1);


                            break;

                    }
                    return true;
                }

            };

我期望的结果是每个底部导航项都将具有其选项卡

bottomNav第一种情况-(家,陆地,海洋)标签 bottomNav第二种情况-(水果,蔬菜)标签 bottomNav 3号保护套-(自行车,汽车,自行车,电影)选项卡 ....

0 个答案:

没有答案