Inflating AppBarLayout with Toolbar + TabLayout

前端 未结 7 1118
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 23:21

I currently have a DrawerLayout in my main.xml. There\'s a Toolbar wrapped in an AppBarLayout, and then a simple LinearLayout to swap out fragments.

One of the fragment

7条回答
  •  無奈伤痛
    2021-01-30 23:32

    To fix my problem I ended up putting the Toolbar, TabLayout, and ViewPager all in my MainActivity.

    main_activity.xml:

    
    
        
    
            
    
                
    
                
    
                
            
    
            
    
        
    
        
    
    
    

    Then, in all of my fragments, I set the visibility for the TabLayout and the ViewPager programmatically in onCreateView:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabLayout);
            tabLayout.setVisibility(View.GONE);
            ViewPager mViewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
            mViewPager.setVisibility(View.GONE);
    
            return inflater.inflate(R.layout.fragment_layout, container, false);
    }
    

    Of course, in the fragment with tabs, you would want to set the visibility to View.VISIBLE instead of View.GONE.

提交回复
热议问题