单击导航抽屉上的项目时如何 hide/disable TabLayouts?

How to hide/disable TabLayouts when items on navigation drawer is clicked?

我对 android 开发还很陌生,因此,我无法通过其他互联网资源的帮助来解决我的问题。使用 Youtube 视频,我正在设计一个同时使用 NavigationDrawerTabLayout 的应用程序,并且所有 TabLayout 项目和导航抽屉项目都使用片段。我的问题是,当我点击导航抽屉中的项目时,应用程序仍然显示 TabLayout 的片段,而不是显示与来自 `NavigationDrawer.click 的项目对应的片段。请帮我解决一下这个。我在下面包含了代码,甚至还有一张图片。非常感谢。

The abovementioned image.

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //following gives toolbar and navigation bar can be viewed by sliding L-R
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);
        //attach sectionpageadapter to viewpager
        SectionPagerAdapter pagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
        ViewPager pager = findViewById(R.id.pager);
        pager.setAdapter(pagerAdapter);

        //attach viewpager to tablayout
        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(pager);


        //following to give hamburger sign to navigation drawer
        drawer = findViewById(R.id.drawer_layout);

        //references to listen to click events on navigation views
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //following for hamburger sign to rotate when slide from L-R
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,toolbar,
                R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        //below is our default fragment which will be shown on create, without any clicks
        //if statement so that it wont reload on rotation or resume of the device
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            navigationView.setCheckedItem(R.id.nav_home);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            break;
            case R.id.nav_myaccount:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new AccountFragment()).commit();
                break;
            case R.id.nav_mymessages:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new MessageFragment()).commit();
                break;
            case R.id.nav_mysongs:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new MySongFragment()).commit();
                break;
            case R.id.nav_mystores:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new StoreFragment()).commit();
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    //following for closing navagation bar instead of leaving activity immediately  when you press back button
    public void onBackPressed() {
        if(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }else {
            super.onBackPressed();
        }
    }
    private class SectionPagerAdapter extends FragmentPagerAdapter{
        public SectionPagerAdapter(FragmentManager fm){
            super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new HomeFragment();
                case 1:
                    return new FragmentArtists();
                case 2:
                    return new FragmentAlbum();
                case 3:
                    return new FragmentPlaylists();
            }
            return null;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:
                    return getResources().getText(R.string.home_tab);
                case 1:
                    return getResources().getText(R.string.artist_tab);
                case 2:
                    return getResources().getText(R.string.album_tab);
                case 3:
                    return getResources().getText(R.string.playlist_tab);
            }
            return null;
        }
    }
}

替换此代码!

 @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_home:
                pager.setVisibility(View.VISIBLE);
                tabs.setVisibility(View.VISIBLE);
                fragmentConterner.setVisibility(View.GONE);

                break;
            case R.id.nav_myaccount:
                pager.setVisibility(View.GONE);
                tabs.setVisibility(View.GONE);
                fragmentConterner.setVisibility(View.VISIBLE);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new AccountFragment()).commit();
                break;
            case R.id.nav_mymessages:
                pager.setVisibility(View.GONE);
                tabs.setVisibility(View.GONE);
                fragmentConterner.setVisibility(View.VISIBLE);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new MessageFragment()).commit();
                break;
            case R.id.nav_mysongs:
                pager.setVisibility(View.GONE);
                tabs.setVisibility(View.GONE);
                fragmentConterner.setVisibility(View.VISIBLE);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new MySongFragment()).commit();
                break;
            case R.id.nav_mystores:
                pager.setVisibility(View.GONE);
                tabs.setVisibility(View.GONE);
                fragmentConterner.setVisibility(View.VISIBLE);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new StoreFragment()).commit();
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

希望对你有所帮助!

您可以将布局元素的可见性设置为可见、不可见和消失。以编程方式,您可以通过

ViewPager.setVisibility(View.GONE);
TabLayout.setVisibility(View.GONE);

并且您还可以显示您用来扩展 FrameLayout 容器的 FrameLayout。

FrameLayout.setVisibility(View.VISIBLE);

首先在 XML 属性中将 FrameLayout 设置为消失。

android:visibility="gone"

希望对您有所帮助。