在扩展片段一类标签 [英] Tabs in a class that extends fragment
问题描述
我读很多教程如何实现在扩展一个类标签 FragmentActivity
,结果却没有一个有关如何实现在扩展一个类的标签片段
搜索结果
我AndroidStudio创建一个新的项目 NavigationDrawerFragment
:这个结果对于navigationDrawerFragment它显示创建一个新片段的每一个选项菜单。这里面我片段需要有一些标签。搜索结果
如何添加标签 onCreateView
片段的方法中?结果
有人可以给我一个很简单的例子?
结果搜索结果
我在做什么来试图解决我的问题:搜索结果
第1步 - 我创建了一个新的的LinearLayout
,它包含 tabHost
和它的主片段:结果
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /片段1
机器人:方向=垂直的android:layout_width =match_parent
机器人:layout_height =match_parent> < TabHost
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / tabHost
机器人:layout_gravity =CENTER_HORIZONTAL> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> < TabWidget
机器人:ID =@机器人:ID /标签
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< / TabWidget>
<的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <的LinearLayout
机器人:ID =@ + ID / TAB1
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / TAB2
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / TAB3
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
< / LinearLayout中>
< /&的FrameLayout GT;
< / LinearLayout中>
< / TabHost>
< / LinearLayout中>
第2步 - 在主片段我把code这行:
公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
//查看rootView = inflater.inflate(R.layout.vediamo,集装箱,FALSE); mTabHost =新FragmentTabHost(getActivity());
mTabHost.setup(getActivity(),getChildFragmentManager(),R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec(简单)。setIndicator(简单),
CreaAnnoFragment.class,NULL);
mTabHost.addTab(mTabHost.newTabSpec(联系人)。setIndicator(联系人),
ModificaAnnoFragment.class,NULL);
mTabHost.addTab(mTabHost.newTabSpec(油门)。setIndicator(节气门),
EliminaAnnoFragment.class,NULL); 返回mTabHost;
} @覆盖
公共无效onDestroyView(){
super.onDestroyView();
mTabHost = NULL;
}
如果我运行程序我得到这个错误:
显示java.lang.NullPointerException
在myapps.studentsmarks.CreaAnnoFragment.onAttach(CreaAnnoFragment.java:100)
块引用>固定 - 解决方案结果
包装的片段不需要onAttach()
的方法,这是逻辑。因此,只要将其删除。解决方案有关,你可以使用
FragmentTabHost
类,让你添加标签到你的片段
,就像你的活动做
。下面是一个例子:
公共类FragmentTabsFragmentSupport扩展片段{
私人FragmentTabHost mTabHost;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
mTabHost =新FragmentTabHost(getActivity());
mTabHost.setup(getActivity(),getChildFragmentManager(),R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec(简单)。setIndicator(简单),
FirstFragment.class,NULL);
mTabHost.addTab(mTabHost.newTabSpec(联系人)。setIndicator(联系人),
SecondFragment.class,NULL);
mTabHost.addTab(mTabHost.newTabSpec(油门)。setIndicator(节气门),
ThirdFragment.class,NULL); 返回mTabHost;
}@覆盖
公共无效onDestroyView(){
super.onDestroyView();
mTabHost = NULL;
}}
有关更多信息,请查看官方文档
I'm reading many tutorial how to implement tabs in a class that extends
FragmentActivity
,
but no one about how to implement tabs in a class that extendsFragment
I created with AndroidStudio a new project with aNavigationDrawerFragment
:
this navigationDrawerFragment for every option menu that it displays create a new Fragment. Inside this fragment i need to have some tabs.
how can i add tabs insideonCreateView
method of the fragment?
someone can give to me a very simple example?
What i'm doing to try to solve my problem:
Step 1 - i created a newLinearLayout
, it contains thetabHost
and it's the main fragment:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tabHost" android:layout_gravity="center_horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
Step 2 - on the main fragment I put this rows of code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //View rootView = inflater.inflate(R.layout.vediamo, container, false); mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), CreaAnnoFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), ModificaAnnoFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), EliminaAnnoFragment.class, null); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; }
If i run the program i get this error:
java.lang.NullPointerException at myapps.studentsmarks.CreaAnnoFragment.onAttach(CreaAnnoFragment.java:100)
FIXED - solution
"the wrapped fragment" don't need theonAttach()
method and this is logic. So just delete it解决方案For that you can use
FragmentTabHost
class, that let's you add tabs to yourFragment
, just as you do forActivities
.Here's an example:
public class FragmentTabsFragmentSupport extends Fragment { private FragmentTabHost mTabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FirstFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), SecondFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), ThirdFragment.class, null); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; }
}
For more information, check the official documentation.
这篇关于在扩展片段一类标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!