2016-12-13 15 views
0

フレームレイアウトをアクティビティに配置し、フラグメント数をフレームレイアウトに配置します。tabLayoutスイッチの遅延とビューも遅延を表示する

私のカスタムの引き出しメニューでは、その断片が賞味できません。

フラグメントの1つにtabLayoutと4つのタブがあります。

私の問題は、私がフラグメントに戻ったときに、タブがデアリーを示すことです。

私はコードを入れようとしています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を持っているので、私はこのようなビューページコードをchabgeします: ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter (getChildFragmentManager()、tabLayoutHomePage.getTabCount());

関連する問題