【问题标题】:FragmentTabHost not creating view inside Fragment in androidFragmentTabHost 没有在 android 的 Fragment 内创建视图
【发布时间】:2014-01-11 15:47:11
【问题描述】:

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

据我所知,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;
    }
}

【问题讨论】:

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


    【解决方案1】:

    根据the docs

    允许在其选项卡中使用 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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      现在我们可以使用 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();
          }
      

      【讨论】:

        【解决方案3】:

        检查这个和平的代码。它可以帮助你:

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

        【讨论】:

        • 无法解析 getTabHost() 和 getTabWidget()。该怎么办。我正在做的是我有一个 ActionbarActivity,此活动的 xml 包含一个片段标记,其名称设置为 Change_password 类,并且 Change_password 类具有您提供的代码。帮帮我
        猜你喜欢
        • 1970-01-01
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-22
        • 1970-01-01
        相关资源
        最近更新 更多