19

我在让选项卡主机上的视图更改时遇到问题 - 当我选择一个选项卡时,内容保持空白。

据我所知,onCreateView没有调用子 FragmentsonMenuCreate运行良好,因为菜单按预期更改。

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

3 回答 3

5

根据文档

允许使用 Fragment 对象作为其选项卡内容的特殊 TabHost。将其放置在视图层次结构中时,膨胀层次结构后,您必须调用 setup(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;
    }
}
于 2014-01-11T16:14:21.837 回答
0

现在我们可以使用 TabLayout 和 ViewPager 来做这些事情了。这是一个很好的使用指南。这是我的代码:

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();
    }
于 2015-09-11T08:04:11.610 回答
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);
        }
于 2015-09-11T10:44:46.070 回答