首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >FragmentTabHost未在android中创建片段内的视图

FragmentTabHost未在android中创建片段内的视图
EN

Stack Overflow用户
提问于 2014-01-11 23:47:12
回答 3查看 7.8K关注 0票数 19

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

据我所知,在子片段上没有调用onCreateView onMenuCreate运行得很好,因为菜单会按预期的方式变化。

代码语言:javascript
复制
   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;
    }
}
EN

回答 3

Stack Overflow用户

发布于 2014-01-12 00:14:22

根据the docs的说法

允许将片段对象用于其选项卡内容的

特殊TabHost。将其放在视图层次结构中时,在膨胀层次结构后,必须调用(Context,FragmentManager,int)来完成选项卡主机的初始化。

(强调我的)

所以我建议这样做:

代码语言:javascript
复制
   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;
    }
}
票数 5
EN

Stack Overflow用户

发布于 2015-09-11 16:04:12

现在我们可以使用TabLayout和ViewPager来做这些事情了。This is a good guide to use it.Here是我的代码:

代码语言:javascript
复制
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();
    }
票数 0
EN

Stack Overflow用户

发布于 2015-09-11 18:44:46

检查这段代码的和平。它可能会对您有所帮助:

代码语言:javascript
复制
        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);
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21064298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档