2015-06-03 107 views
4

我正在使用新的Android支持設計庫。我遵循Android Developer Blogspot上的演示,除了使用RecyclerView的片段而不是將它們放在同一個xml中。AppBarLayout沒有響應滾動片段

這裏是我的activity_main.xml中

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

    <android.support.design.widget.CoordinatorLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 

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

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

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

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



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


     <fragment 
      android:id="@+id/navigation_drawer" 
      android:layout_marginTop="?android:attr/actionBarSize" 
      android:name="aungkyawpaing.yangonuniversity.Fragments.NavigationDrawerFragment" 
      android:layout_width="@dimen/navigation_drawer_width" 
      android:layout_height="match_parent" 
      android:layout_gravity="start|left" 
      tools:layout="@layout/fragment_navigation_drawer"/> 

</android.support.v4.widget.DrawerLayout> 

這是toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top" 
    android:background="?attr/colorPrimary" 
    android:minHeight="?attr/actionBarSize" 
    app:layout_scrollFlags="scroll|enterAlways" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 


And this is the fragment where it has RecyclerView 

    <android.support.v7.widget.RecyclerView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/department_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
     /> 

我加入gradle這個最新的支持庫的依賴。我看到另一個人說它不起作用,因爲他在片段中使用了列表視圖,但是我按照建議使用了RecyclerView。

回答

7

好吧,這可能看起來愚蠢的,但你必須在依賴添加此

compile 'com.android.support:recyclerview-v7:22.2.0' 
+0

是的,謝謝! – gphilip

-1

你的FrameLayout需要由NestedScrollView包裹,因爲FrameLayout裏不處理行爲類。