TabHost无法处理片段

时间:2017-03-27 08:12:40

标签: android android-fragments android-tabhost

我已设法在TabHost上添加fragment,但当我点击一个标签时,我希望另一个fragment进入FrameLayout,同样会转到另一个。{ / p>

我尝试过应用该方法,但在setup 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;


 }


}

1 个答案:

答案 0 :(得分:0)

HomeFragment onCreateView()改为

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 ); 
    } 
}

它应该可以正常工作。