FragmentTabHost not creating view inside Fragment in android

与世无争的帅哥 提交于 2019-12-08 14:37:53

问题


I am having an issue getting the view to change on a tabhost - when I select a tab the content stays blank.

From what I can tell, onCreateView is not being called on the child Fragments. onMenuCreate runs fine because the menu changes like it is supposed to.

   public class PatientTabFragment extends Fragment {
    private FragmentTabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager());

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
                NewPatientFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
                NoteListFragment.class, null);


        return mTabHost;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}

回答1:


according to the docs:

Special TabHost that allows the use of Fragment objects for its tab content. When placing this in a view hierarchy, after inflating the hierarchy you must call setup(Context, FragmentManager, int) to complete the initialization of the tab host.

(emphasis mine)

So I suggest somethong like this:

   public class PatientTabFragment extends Fragment {
    private FragmentTabHost mTabHost;
    private boolean createdTab = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager());

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
                NewPatientFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
                NoteListFragment.class, null);


        return mTabHost;
    }

    public void onResume(){
        if (!createdTab){
          createdTab = true;
          mTabHost.setup(getActivity(), getActivity().
                         getSupportedFragmentManager());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}



回答2:


Now we can use TabLayout and ViewPager do those things.This is a good guide to use it.Here is my code:

viewPager=(NonSwipeableViewPager)view.findViewById(R.id.circleresdyn_viewpager);
    tabLayout=(TabLayout)view.findViewById(R.id.circleresdyn_tablayout);

    if (viewPager != null) {
        Adapter adapter = new Adapter(((AppCompatActivity)activity).getSupportFragmentManager());
        ContentFragment con=new ContentFragment();
        con.setArguments(bundleForFramgnet);
        MemberFragment memberFragment=new MemberFragment();
        memberFragment.setArguments(bundleForFramgnet);
        CirResDynTileFragment cirResDynTileFragment=new CirResDynTileFragment();
        cirResDynTileFragment.setArguments(bundleForFramgnet);
        adapter.addFragment(cirResDynTileFragment, "Tab1");
        adapter.addFragment(con, "Tab2");
        adapter.addFragment(memberFragment, "Tab3");
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).select();
    }



回答3:


Check this peace of code. It may help you:

        import android.app.Fragment;

        public class Change_password extends Fragment {


            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.change_password, container,false);
setTabs();

        return rootView;
            }



        private void setTabs() {
            try {

                addTab("Airlines", R.drawable.tab_home, HomeActivity_bkp.class);
                addTab("Advance Search", R.drawable.tab_search,
                        AdvanceSearchAcitivty.class);

                addTab("Booking", R.drawable.tab_home, Booking.class);
                addTab("Settings", R.drawable.tab_search, SettingAcitivty.class);

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
                // TODO: handle exception
            }
        }

        private void addTab(String labelId, int drawableId, Class<?> c) {
            TabHost tabHost = getTabHost();

            Intent intent = new Intent(this, c);
            TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

            View tabIndicator = LayoutInflater.from(this).inflate(
                    R.layout.tab_indicator, getTabWidget(), false);
            TextView title = (TextView) tabIndicator.findViewById(R.id.title);
            title.setText(labelId);
            ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
            icon.setImageResource(drawableId);

            spec.setIndicator(tabIndicator);
            spec.setContent(intent);
            tabHost.addTab(spec);
        }


来源:https://stackoverflow.com/questions/21064298/fragmenttabhost-not-creating-view-inside-fragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!