FragmentTabHost - No tab known for tag null

后端 未结 3 808
执笔经年
执笔经年 2020-12-29 07:22

Few days ago I implemented TabHostFramgent in a Fragment with a NavigationDrawer, and I faced with a problem that is the following err

3条回答
  •  情歌与酒
    2020-12-29 08:02

    There is another way to display tabs which can be used here.

    Define TabHostFragment like this:

    public class TabHostFragment extends Fragment implements ActionBar.TabListener{
    
        public TabHostFragment(){
    
        }
    
        AppSectionsPagerAdapter mAppSectionsPagerAdapter;
        ViewPager mViewPager;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.tab_host_test2, container, false);
            return rootView;
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState){
            mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getChildFragmentManager());
    
            final ActionBar actionBar = ((FragmentActivity)getActivity()).getSupportActionBar();
    
            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            mViewPager = (ViewPager) view.findViewById(R.id.pager);
            mViewPager.setAdapter(mAppSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    
            for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
                actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
            }
        }
    
        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        }
    
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            mViewPager.setCurrentItem(tab.getPosition());
        }
    
        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        }
    
        public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
    
            public AppSectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int i) {
                Fragment f = null;
                switch (i) {
                    case 0:
                        f = new MisOfertasFragment();
                        break;
    
                    case 1:
                        f = new RecomendacionesFragment();
                        break;
                }
    
                return f;
            }
    
            @Override
            public int getCount() {
                return 2;
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                return getFragmentTitle(position);
            }
    
            private String getFragmentTitle(int position){
                if(position == 0)
                    return "Tab 1";
                else if(position == 1)
                    return "Tab 2";
                else
                    return "";
            }
        }
    }
    

    Define tab_host_test2.xml like this:

    
    

    Rest of the code remains the same.

    Try this. This should work.

    EDIT:

    Add this to the displayView() method:

    if(position != 0)
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    

提交回复
热议问题