则tabspec SetContent到碎片 [英] TabSpec SetContent to Fragment
本文介绍了则tabspec SetContent到碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用TabHost内片段创建标签。我遇到的问题是则tabspec的 setContent()
。我需要使之巢℃在另一个片段设置;&的FrameLayout GT;
。
我使用 getChildFragmentManager()
来做到这一点?我怎么做呢?
XML布局:
<的LinearLayout的android:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
> < TabWidget机器人:ID =@机器人:ID /标签
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =0
机器人:方向=横向
/> <的FrameLayout机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =match_parent
机器人:layout_height =0dp/> <的FrameLayout机器人:ID =@ + ID /下面的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/> <的FrameLayout机器人:ID =@ + ID /你
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>< / LinearLayout中>
片段类:
公共类ActivityFragment扩展片段实现TabHost.OnTabChangeListener {
私有静态最后弦乐FOLLOWING_SPEC =下面的;
私有静态最后弦乐YOU_SPEC =你;私人TabHost tabHost;公共ActivityFragment(){}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.fragment_activity,集装箱,FALSE); tabHost =(TabHost)view.findViewById(android.R.id.tabhost);
tabHost.setup(); TabHost.TabSpec followingSpec = tabHost.newTabSpec(FOLLOWING_SPEC);
followingSpec.setIndicator(继);
followingSpec.setContent(); //< ===需要设置的内容片断 TabHost.TabSpec youSpec = tabHost.newTabSpec(YOU_SPEC);
youSpec.setIndicator(您); tabHost.addTab(followingSpec);
tabHost.addTab(youSpec);
tabHost.setCurrentTab(0); 返回视图。
}@覆盖
公共无效onTabChanged(String s)将{ }
}
解决方案
下面的答案。我是混合Android的TabHost的支持库的FragmentTabHost。两个不同的东西!
公共类ActivityFragment扩展片段实现TabHost.OnTabChangeListener {
私有静态最后弦乐FOLLOWING_SPEC =下面的;
私有静态最后弦乐YOU_SPEC =你;私人FragmentTabHost tabHost;公共ActivityFragment(){}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.fragment_activity,集装箱,FALSE); tabHost =(FragmentTabHost)view.findViewById(R.id.tabhost);
tabHost.setup(getActivity(),getChildFragmentManager(),android.R.id.tabcontent); tabHost.addTab(tabHost.newTabSpec(FOLLOWING_SPEC).setIndicator(继),FollowingActivityFragment.class,NULL);
tabHost.addTab(tabHost.newTabSpec(YOU_SPEC).setIndicator(您),YouActivityFragment.class,NULL);
tabHost.setCurrentTab(0); 返回视图。
} @覆盖
公共无效onTabChanged(String s)将{ }
}
I am using TabHost inside Fragment to create tabs. The problem I run into is TabSpec's setContent()
. I need to set it so that it nests another fragment under the <FrameLayout>
.
Do I uses getChildFragmentManager()
to do this? How do I do so?
Xml Layout:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<FrameLayout android:id="@+id/following"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<FrameLayout android:id="@+id/you"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Fragment class:
public class ActivityFragment extends Fragment implements TabHost.OnTabChangeListener {
private static final String FOLLOWING_SPEC = "following";
private static final String YOU_SPEC = "you";
private TabHost tabHost;
public ActivityFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_activity, container, false);
tabHost = (TabHost)view.findViewById(android.R.id.tabhost);
tabHost.setup();
TabHost.TabSpec followingSpec = tabHost.newTabSpec(FOLLOWING_SPEC);
followingSpec.setIndicator("Following");
followingSpec.setContent(); // <===Need to set content to fragment
TabHost.TabSpec youSpec = tabHost.newTabSpec(YOU_SPEC);
youSpec.setIndicator("You");
tabHost.addTab(followingSpec);
tabHost.addTab(youSpec);
tabHost.setCurrentTab(0);
return view;
}
@Override
public void onTabChanged(String s) {
}
}
解决方案
Here's the answer. I was mixing android's TabHost with the support library's FragmentTabHost. Two different things!
public class ActivityFragment extends Fragment implements TabHost.OnTabChangeListener {
private static final String FOLLOWING_SPEC = "following";
private static final String YOU_SPEC = "you";
private FragmentTabHost tabHost;
public ActivityFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_activity, container, false);
tabHost = (FragmentTabHost)view.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec(FOLLOWING_SPEC).setIndicator("Following"), FollowingActivityFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(YOU_SPEC).setIndicator("You"), YouActivityFragment.class, null);
tabHost.setCurrentTab(0);
return view;
}
@Override
public void onTabChanged(String s) {
}
}
这篇关于则tabspec SetContent到碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文