2013-10-24 36 views
0

我有一個活動的根佈局是一個抽屜佈局,主要內容是一個視圖分頁器(和com.astuetz.viewpager.extensions.PagerSlidingTabStrip協助viewpager。現在我想要做的是在導航抽屜選擇特定項目,視圖尋呼機(與標籤名條沿)獲取一個片段取代我的活動佈局文件是這樣的:交換ViewPager與單個片段

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

當前正在發生的是新的片段元素與視圖尋呼機重疊,而我仍然能夠在後臺使用視圖尋呼機。請幫我弄明白。

回答

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讓您的片段交易實際上會做更換 - 這取決於你的應用程序是如何架構,這可能會更有意義。

+0

這種能見度的工作原理! :D 雖然如你所說,你的第二個想法對我來說也更有意義。當我得到時間並讓你知道它是如何發生的時候,我會試試。 (我會接受你的答案後,我試試這個:P) – gitter

+1

它的工作原理:D謝謝 – gitter