帮助大家。请知道如何在片段中构建tabhost。我想要以下层次结构:naviagation抽屉 - >片段和片段我加载tabhost。如果有人能帮助我,我等着
答案 0 :(得分:1)
您应该使用TabLayout而不是TabHost。
为主要片段制作Xml,内容是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>
然后按如下所示制作代码:
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);
}
}
}
答案 1 :(得分:0)
我假设您已经实现了导航抽屉,并且在片段中调用tabhost时遇到了麻烦。
请尝试以下步骤。
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;
}
}