2016-12-13 124 views
0

我把框架佈局放在一個活動中,並把數量的碎片放入框架佈局。tabLayout開關延遲和視圖顯示延遲

我的自定義Drawer菜單中的片段swith。

碎片之一,它有一個tabLayout和四個選項卡切換。

所以我的問題是當我切換回片段,選項卡顯示dealy。

我嘗試把代碼transaction.addToBackStack(null);它不起作用。

請教我如何解決這個延遲,謝謝。

這裏是我的代碼:

@Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 

     int id = item.getItemId(); 
    FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
    if (id == R.id.homePage) { 
      Homepage homepageFragment = new Homepage(); 
      transaction.addToBackStack(null); 
      transaction.replace(R.id.mainFrame, homepageFragment, null); 
      setTitleBar(R.string.appTitle); 
      transaction.commit(); 

     } else if (id == R.id.newestInformation) { 
      NewsInformation newsFragment = new NewsInformation(); 
      transaction.addToBackStack(null); 
      transaction.replace(R.id.mainFrame, newsFragment, null); 
      setTitleBar(R.string.newestInformation); 
      transaction.commit(); 
     } 

//的setContentView

FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction(); 
Homepage homepageFragment = new Homepage(); 
Fragment fragment = manager.findFragmentById(R.id.mainFrame); 
if (fragment == null) { 
    transaction.addToBackStack(null); 
    transaction.add(R.id.mainFrame, homepageFragment, null); 
    setTitleBar(R.string.appTitle); 
    transaction.commit(); 
} else { 
    Log.i(TAG, ">>>" + "homepageFragment!=null"); 
} 

// XML

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF"> 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/id_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:theme="@style/ToolBarStyle" 
      android:background="@color/colorAccent"> 

      <TextView 
       android:id="@+id/textTitle" 
       android:textSize="18dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="@android:color/background_light" /> 

     </android.support.v7.widget.Toolbar> 

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

    </android.support.design.widget.AppBarLayout> 


    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@android:color/white" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header" 
     app:menu="@menu/drawer_menu" /> 
</android.support.v4.widget.DrawerLayout> 

//片段XML

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white"> 

    <android.support.design.widget.TabLayout 
     android:layout_width="match_parent" 
     android:id="@+id/tabLayoutHomePage" 
     android:layout_weight="1" 
     app:tabTextColor="@android:color/white" 
     android:background="@color/colorPrimary" 
     android:layout_height="80dp"> 

    </android.support.design.widget.TabLayout> 

    <view 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     class="android.support.v4.view.ViewPager" 
     android:id="@+id/viewPager" 
     android:layout_weight="1" /> 

</android.support.design.widget.AppBarLayout> 

回答

0

我找到了解決方案,因爲我的ViewPager在嵌套片段上。 我必須getChildFragmentManager,所以我查看這樣的viewpager代碼: ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter (getChildFragmentManager(),tabLayoutHomePage.getTabCount());