问题
helle everyone. please Iwant to know how to build tabhost in a fragment. I want to have the following hierarchy: naviagation drawer -> fragment and in the fragment I load the tabhost. if anyone can help me I waiting please
回答1:
you can do like this.
You should use TabLayout instead of TabHost.
Make Xml for main fragment which content Tabs.
<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>
Then Make Code like below:
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);
}
}
}
回答2:
I am assuming that you have implemented Navigation Drawer, and having trouble in calling tabhost in fragment.
Try the following steps.
- Create a parent fragment to hold the tab, in this fragment add as many tabs as you want to add. (In this example I am showing two tabs).
- Create the fragments that you have added in the tabs. (Fragment1, Fragment2)
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;
}
}
来源:https://stackoverflow.com/questions/38864778/how-to-load-tabhost-in-a-fragment