FragmentTabHost没有在android中的Fragment中创建视图

时间:2014-01-11 15:47:12

标签: android android-fragments android-tabhost fragment-tab-host android-nested-fragment

我在使用tabhost更改视图时遇到问题 - 当我选择一个标签时,内容会保持空白。

据我所知, onCreateView未在儿童片段上被调用。 onMenuCreate运行正常,因为菜单会像预期的那样发生变化。

   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;
    }
}

3 个答案:

答案 0 :(得分:5)

根据the docs

  

特殊TabHost,允许将Fragment对象用于其选项卡   内容。将此放置在视图层次结构中时, 在充气后   您必须将层次结构(Context,FragmentManager,int)调用到的层次结构   完成标签主机的初始化。

(强调我的)

所以我建议像这样:

   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;
    }
}

答案 1 :(得分:0)

现在我们可以使用TabLayout和ViewPager执行这些操作。This is a good guide to use it。这是我的代码:

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();
    }

答案 2 :(得分:0)

检查代码的安静。它可能对你有所帮助:

        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);
        }