0

我有一个活动,其根布局是抽屉布局,主要内容是视图寻呼机(以及 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>

现在这就是我试图用片段替换视图寻呼机。我自己对此不确定,有点困惑将哪个元素放入替换方法。

UserProfileActivity userProfileFragment = new UserProfileActivity();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentToSwap, userProfileFragment);
transaction.addToBackStack(null);
transaction.commit();

当前正在发生的是新的片段元素与视图寻呼机重叠,而我仍然能够在后台使用视图寻呼机。请帮我弄清楚。

4

1 回答 1

2

该片段与 ViewPager 重叠,因为它们都在一个 RelativeLayout 中 - 如果您想隐藏 ViewPager 和选项卡条,您可以对它们中的每一个调用 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 和选项卡条放在它自己的片段中,并将其附加到 fragmentToSwap FrameLayout 以便您的片段事务实际上会进行替换 - 取决于您的应用程序的架构方式,这可能更有意义.

于 2013-10-24T02:22:30.737 回答