2016-09-13 46 views
0

我實施了this作爲默認片段,當導航抽屜啓動並且它工作時,但不會出現ActionBarDrawerToggleActionBarDrawerToggle不會出現在片段

主要活動:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ 

private ActionBarDrawerToggle toggle; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Initializing Toolbar and setting it as the actionbar 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    ServiceFragment serviceFragment = new ServiceFragment(); 
    FragmentManager manager = getSupportFragmentManager(); 
    manager.beginTransaction() 
      .replace(R.id.content_fragment, serviceFragment) 
      .commit(); 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

    toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    //Initializing NavigationView 
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 

    //This method triggers when an item is clicked in navigation drawer menu 
    navigationView.setNavigationItemSelectedListener(this); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<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/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    android:id="@+id/content_fragment"/> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

默認片段,ServiceFragment膨脹佈局fragment_service.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/service_fragment"> 

<!-- TODO: Update blank fragment layout --> 
    <android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     android:fitsSystemWindows="true" 
     > 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/detail_backdrop_height" 
      android:fitsSystemWindows="true" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsing_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fitsSystemWindows="true" 
       app:contentScrim="?attr/colorPrimary" 
       app:expandedTitleMarginEnd="64dp" 
       app:expandedTitleMarginStart="48dp" 
       app:expandedTitleTextAppearance="@android:color/transparent" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 
        <!--android:scaleType="centerCrop"--> 
        <ImageView 
         android:id="@+id/backdrop" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:fitsSystemWindows="true" 
         android:scaleType="fitXY" 
         app:layout_collapseMode="parallax" /> 

       </RelativeLayout> 
       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:background="?attr/colorPrimary" 
        app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

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

     <include layout="@layout/recyclerview"/> 

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

+0

這是因爲你的'Toolbar'現在還沒有當你創建'ActionBarDrawerToggle'。實際上,'toolbar'在整個'onCreate()'中都是​​空的,但是'ActionBarDrawerToggle'不會抱怨它,所以不會拋出異常。你應該把'DrawerLayout','Toolbar'和'ActionBarDrawerToggle'放在一個地方 - Activity或者'Fragment' - 但是如果你真的想保持當前的設置,調用'manager.executePendingTransactions ()'在'commit()'調用之後就可能解決你的問題。 –

+0

在Fragment中添加'DrawerLayout','Toolbar'和'ActionBarDrawerToggle'確實[work](http://imgur.com/a/MEhe1),但當我向下滾動頁面時,Action欄不會出現 - 摺疊工具欄 - 如上圖所示。通過調用'manager.executePendingTransactions()'無效。有任何想法嗎? – xaldin

+0

哦,對不起,我忽略了提到你必須將'toolbar'初始化移動到'executePendingTransactions()'調用之後。基本上,在'setContentView()'之後立即執行'FragmentTransaction',然後執行其他所有操作。 –

回答

0

您需要添加工具欄如下:

<?xml version="1.0" encoding="utf-8"?> 
    <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/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 
     <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       /> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/content_fragment"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

&在片段佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:clickable="true" 
    android:background="@android:color/white" 
    > 

     <include layout="@layout/recyclerview"/> 

     </LinearLayout>