Android 使用单个片段交换ViewPager

Android 使用单个片段交换ViewPager,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我有一个活动,它的根布局是抽屉布局,主要内容是查看页面(com.astuetz.viewpager.extensions.PagerSlidingTabStrip用于辅助查看页面。现在我想做的是,当在导航抽屉中选择特定项目时,查看页面(以及选项卡名称条)被单个片段替换。我的活动布局文件如下: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/androi

我有一个活动,它的根布局是抽屉布局,主要内容是查看页面(com.astuetz.viewpager.extensions.PagerSlidingTabStrip用于辅助查看页面。现在我想做的是,当在导航抽屉中选择特定项目时,查看页面(以及选项卡名称条)被单个片段替换。我的活动布局文件如下:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.astuetz.viewpager.extensions.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dip"
            android:background="@drawable/background_tab"
            app:underlineColor="#E67E22"
            app:indicatorColor="#E67E22" />

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tabs"
            tools:context=".UserActivity" >
        </android.support.v4.view.ViewPager>


        <FrameLayout android:id="@+id/fragmentToSwap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    </RelativeLayout>

    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:background="#E8E8E8"
        />
</android.support.v4.widget.DrawerLayout>

当前发生的情况是,新的片段元素与视图页面重叠,而我仍然可以在后台使用视图页面。请帮助我解决这个问题。

片段与视图页面重叠,因为它们都位于相对位置-如果要隐藏视图页面和选项卡条,可以调用setVisibility(View.GONE)来隐藏它们

// assuming you do not already have a reference to them, 
// find them by their ID and hide them
findViewById(R.id.tabs).setVisibility(View.GONE);
findViewById(R.id.pager).setVisibility(View.GONE);

编辑:实际上,你可以将ViewPager和tab strip放在它自己的片段中,并将其附加到fragmentToSwap FrameLayout上,这样你的片段事务就可以实际进行替换-这取决于你的应用程序的架构,这可能更有意义。

可视性工作原理!:D尽管如你所说,这是你的第二个想法这对我来说也更有意义。我会在有时间的时候试一下,让你知道事情的进展。(试过这个后,我会接受你的答案:P)
// assuming you do not already have a reference to them, 
// find them by their ID and hide them
findViewById(R.id.tabs).setVisibility(View.GONE);
findViewById(R.id.pager).setVisibility(View.GONE);