2015-07-18 38 views
-1

我已经尝试了几篇不同的文章,这里有很多线索和教程,但我无法得到这个做任何事情。AppBarLayout拒绝做任何事

在我的代码中,使用此布局的活动是AppCompatActivity。 我有AppCompatTheme.Light.NoActionBar

在我的活动内我设置setSupportActionBar()到视图R.id.toolbar

这是我的目标:http://imgur.com/Hl2Asb1.gif

// Dependencies 
compile 'com.android.support:appcompat-v7:21.0.3' 
compile 'com.android.support:cardview-v7:21.0.2' 
compile 'com.android.support:recyclerview-v7:21.0.2' 
compile 'com.android.support:appcompat-v7:21.0.3' 
compile 'com.android.support:support-v13:21.0.3' 
compile 'com.android.support:multidex:1.0.0' 
compile 'com.android.support:design:22.2.0' 
compile 'com.squareup.picasso:picasso:2.5.2' 
compile 'com.jakewharton:butterknife:6.1.0' 

这里是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways"/> 
    </android.support.design.widget.AppBarLayout> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/listview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
</android.support.design.widget.CoordinatorLayout> 
+0

你有两个'工具栏'的特殊原因吗?你可以包含你的'build.gradle'文件的依赖关系吗? – ianhanniballake

+0

添加了我的依赖关系。我有两个'工具栏',因为我只是想要得到一些结果。 – Sherlock

+0

我从xml中移除了第二个工具栏,以避免任何进一步的混淆。 – Sherlock

回答

3

您必须使用RecyclerView至少版本22.2.0(目前22.2.1)采取的设计库功能优势,如AppBarLayout

compile 'com.android.support:recyclerview-v7:22.2.1' 

正是在22.2.0是他们添加了对嵌套滚动的支持,这是AppBarLayout依赖的基础功能。

-1

放置RecyclerviewAppBarLayout的顶部像这样 `

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/listview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways"/> 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar2" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways"/> 


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



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

为什么使用两个Toolbars?只是想知道...

+0

更改RecyclerView的位置没有区别。 – Sherlock