选项卡主机无法处理片段



我已经设法在我的fragment上添加了TabHost,但是当我单击一个选项卡时,我希望另一个fragment进入FrameLayout,另一个也是如此。

我尝试应用该方法,但它在设置host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent)中不起作用。

可以做些什么来解决这个问题?

这是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
        <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="30dp" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLinear"
        />
    </TabHost>


</RelativeLayout>

这是我的Java代码:

public class HomeFragment extends Fragment {
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  }
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);

    TabHost host = (TabHost) view.findViewById(R.id.tabHost);
    host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

    host.addTab(host.newTabSpec("tab11").setIndicator("Tab11"), PhotosFragment1.class, null);

    host.addTab(host.newTabSpec("tab22").setIndicator("Tab22"), PhotosFragment2.class, null);

    return view;

 }

}

HomeFragmentonCreateView()更改为

View view = inflater.inflate( R.layout.fragment_home, container, false );
FragmentTabHost host = ( FragmentTabHost ) view.findViewById( R.id.tabHost );
host.setup(getActivity(), getFragmentManager(), R.id.realtabcontent);
host.addTab( host.newTabSpec( "tab11" ).setIndicator( "Tab11" ), PhotosFragment1.class, null );
host.addTab( host.newTabSpec( "tab22" ).setIndicator( "Tab22" ), PhotosFragment2.class, null );
return view;

在你的fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<android.support.v4.app.FragmentTabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">
    <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="30dp"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLinear"/>
</android.support.v4.app.FragmentTabHost>

喜欢PhotosFragment1PhotosFragment2

public class PhotosFragment1 extends Fragment
{
    @Nullable
    @Override
    public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) 
    {
        return View.inflate( getContext(), R.layout.fragment_photos_1, null ); 
    } 
}

它应该工作正常。

最新更新