目标是实现替代ToolBar,它看起来更像'SearchView',例如:在Google Play应用程序中搜索。当ViewPager中的RecyclerView向下滚动时,该视图也应该做出相应的反应(滑开),并在用户滚动列表时滑入。透明AppBarLayout与CardView在其中
下面是截图:
基本上实现视图本身并不复杂的。
<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
似乎是透明的现在):
而不是透明背景,AppBarLayout保持白色。因此,不像第一次截图那样需要透明背景,我因不明原因而变白。
有没有人遇到过这个问题?有没有干净的解决方案来解决这个问题?
P.S.我发现了一些有趣的行为:如果我从ViewPager
中删除app:layout_behavior="@string/appbar_scrolling_view_behavior"
,则AppBarLayout
保持透明,因为即使在滚动后它也应该是透明的。这意味着问题位于ScrollViewBehavior的某处,但我无法找到它。
感谢您的回复。我已经尝试过这种方法,但它并不完全。不设置滚动行为的ViewPager和RecyclerView,不会立即滚动:我想AppBarLayout正在窃取滚动事件,并且只有在滑出屏幕后才会将其返回到RecyclerView,这感觉非常不自然。 –