2016-02-13 71 views
1

目标是实现替代ToolBar,它看起来更像'SearchView',例如:在Google Play应用程序中搜索。当ViewPager中的RecyclerView向下滚动时,该视图也应该做出相应的反应(滑开),并在用户滚动列表时滑入。透明AppBarLayout与CardView在其中

下面是截图:

enter image description here

基本上实现视图本身并不复杂的。

<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.v4.view.ViewPager 
     android:id="@+id/view_pager_menu" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     app:elevation="0dp"> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="48dp" 
      android:layout_margin="8dp" 
      app:layout_scrollFlags="scroll|enterAlways"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
       <!-- Other boring implementation details --> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 

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

    <!-- TabLayout which is used as indicator for ViewPager --> 
    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout_menu" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" /> 

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

好了,结果是这样的滚动下来之前(因为windowBackground被设置为白色,AppBarLayout似乎是透明的现在): enter image description here

但滚动后... enter image description here

而不是透明背景,AppBarLayout保持白色。因此,不像第一次截图那样需要透明背景,我因不明原因而变白。

有没有人遇到过这个问题?有没有干净的解决方案来解决这个问题?

P.S.我发现了一些有趣的行为:如果我从ViewPager中删除app:layout_behavior="@string/appbar_scrolling_view_behavior",则AppBarLayout保持透明,因为即使在滚动后它也应该是透明的。这意味着问题位于ScrollViewBehavior的某处,但我无法找到它。

回答

1

AppBarLayout不是白色的。你所看到的是你活动的窗口背景。

layout_behavior="@string/appbar_scrolling_view_behavior"会影响视图的测量和布局,具体取决于兄弟AppBarLayout。部分影响是抵消视图的垂直位置,使其始终低于AppBarLayout。这就是为什么你看到底下的窗口背景;没有View占据那个空间,因此除了窗户之外没有任何东西被绘制在那里。

看起来你可能不想使用appbar_scrolling_view_behavior,而是只允许它们总是重叠(这是你之前注意到当你删除该行为时)。这提出了在RecyclerView滚动到顶部时如何不覆盖内容的问题,并且您还将AppBarLayout滚动到屏幕上。快速解决方案是将以下属性添加到RecyclerView

<!-- Padding derived from the size and margins of the CardView --> 
android:paddingTop="64dp" 
android:clipToPadding="false" 
+0

感谢您的回复。我已经尝试过这种方法,但它并不完全。不设置滚动行为的ViewPager和RecyclerView,不会立即滚动:我想AppBarLayout正在窃取滚动事件,并且只有在滑出屏幕后才会将其返回到RecyclerView,这感觉非常不自然。 –