如何在片段中加载 tabhost
how to load tabhost in a fragment
大家好。请问我想知道如何在片段中构建 tabhost。我想要具有以下层次结构:导航抽屉 -> 片段,并在片段中加载 tabhost。如果有人可以帮助我,请等待
我假设您已经实现了 Navigation Drawer,并且在片段中调用 tabhost 时遇到问题。
请尝试以下步骤。
- 创建一个父片段来保存选项卡,在此片段中添加任意数量的选项卡。 (在这个例子中,我展示了两个选项卡)。
- 创建您在选项卡中添加的片段。 (片段 1,片段 2)
ParentFragment.java
public class ParentView extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_parent_view);
mTabHost.getTabWidget().setBackgroundColor(getResources().getColor(R.color.color_primary_dark)); // if you want to set color of tab
Bundle arg1 = new Bundle();
arg1.putString("aim", "passingvalues1");
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("NAME1"),
Fragment1.class, arg1);
Bundle arg2 = new Bundle();
arg2.putString("aim", "passingvalues2");
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Name2"),
Fragment2.class, arg2);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
fragment_parent_view.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xx.xx.xx.ParentFragment">
</FrameLayout>
Fragment1.java
public class Fragment1 extends Fragment
{
private LinearLayout llLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
llLayout = (LinearLayout)inflater.inflate(R.layout.fragment1, container, false);
return llLayout;
}
}
Fragment2.java
public class Fragment2 extends Fragment
{
private LinearLayout llLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
llLayout = (LinearLayout)inflater.inflate(R.layout.fragment2, container, false);
return llLayout;
}
}
你可以这样做。
您应该使用 TabLayout 而不是 TabHost。
为包含 Tabs 的主要片段制作 Xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
然后编写如下代码:
public class TabsFragment extends Fragment {
View view;
PagerAdapter adapter;
private void setupViewPager(ViewPager viewPager) {
///Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new Fragment1(), "Fragment1");
adapter.addFragment(new Fragment2(), "Fragment2");
viewPager.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f_tabslayout, container, false);
super.onCreate(savedInstanceState);
final ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
大家好。请问我想知道如何在片段中构建 tabhost。我想要具有以下层次结构:导航抽屉 -> 片段,并在片段中加载 tabhost。如果有人可以帮助我,请等待
我假设您已经实现了 Navigation Drawer,并且在片段中调用 tabhost 时遇到问题。
请尝试以下步骤。
- 创建一个父片段来保存选项卡,在此片段中添加任意数量的选项卡。 (在这个例子中,我展示了两个选项卡)。
- 创建您在选项卡中添加的片段。 (片段 1,片段 2)
ParentFragment.java
public class ParentView extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_parent_view);
mTabHost.getTabWidget().setBackgroundColor(getResources().getColor(R.color.color_primary_dark)); // if you want to set color of tab
Bundle arg1 = new Bundle();
arg1.putString("aim", "passingvalues1");
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("NAME1"),
Fragment1.class, arg1);
Bundle arg2 = new Bundle();
arg2.putString("aim", "passingvalues2");
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Name2"),
Fragment2.class, arg2);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
fragment_parent_view.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xx.xx.xx.ParentFragment">
</FrameLayout>
Fragment1.java
public class Fragment1 extends Fragment
{
private LinearLayout llLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
llLayout = (LinearLayout)inflater.inflate(R.layout.fragment1, container, false);
return llLayout;
}
}
Fragment2.java
public class Fragment2 extends Fragment
{
private LinearLayout llLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
llLayout = (LinearLayout)inflater.inflate(R.layout.fragment2, container, false);
return llLayout;
}
}
你可以这样做。
您应该使用 TabLayout 而不是 TabHost。
为包含 Tabs 的主要片段制作 Xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
然后编写如下代码:
public class TabsFragment extends Fragment {
View view;
PagerAdapter adapter;
private void setupViewPager(ViewPager viewPager) {
///Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new Fragment1(), "Fragment1");
adapter.addFragment(new Fragment2(), "Fragment2");
viewPager.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f_tabslayout, container, false);
super.onCreate(savedInstanceState);
final ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}