我一直在尝试重现滚动列表视图时5.0版上的联系人应用程序折叠工具栏的方式.
展示所需交互的屏幕截图库 请注意工具栏分阶段崩溃,显示搜索+上次联系,淡化上次联系,折叠上次联系,折叠搜索,只留下选项卡.
到目前为止,我在LinearLayout的Recyclerview上方有一个工具栏,工具栏用作操作栏,而不是独立的.
我无法弄清楚如何拦截recyclerview上的触摸事件并使其缩小工具栏,然后将滚动事件返回到recyclerview.我尝试将整个事物放在一个滚动视图中,但然后recyclelerview无法正确计算它的高度并且没有显示任何内容.我尝试在recyclelerview上覆盖onscroll,发现它只会在滚动事件开始时通知我,并为我提供第一个可见的卡ID.
看起来正确的方式,但我不能为我的生活工作,是这样的:
getSupportActionBar().setHideOnContentScrollEnabled(true);
Run Code Online (Sandbox Code Playgroud)
哪个回报:
Caused by: java.lang.UnsupportedOperationException: Hide on content scroll is not supported in this action bar configuration.
Run Code Online (Sandbox Code Playgroud)
使用传统的操作栏,在其下面放置一个工具栏,并设置hideoncontentscrollenabled也不起作用,滚动从不触发操作栏上的隐藏方法.
- 编辑 - 我能够使用传统的操作栏获取hideOnContentScrollEnabled列表视图,但行为与联系人应用程序不同.这显然不是他们使用的方法 - 它只是在listview上发生fling事件时触发操作栏上的.hide(),这与联系人应用程序明显不同,后者会拖动工具栏和滚动事件. - /编辑 -
所以我放弃了那条路线,并将fill_parent放在cardview高度上,并在工具栏上设置了折叠动画.但是如何触发它以使其跟随触摸事件然后将触摸事件返回到recyclerview?
activity_main.xml中
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
/>
<fragment android:name="me.myapplication.FragmentTab"
android:id="@+id/tab_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="8dp"
android:background="#eeeeee"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
styles.xml
...
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> …Run Code Online (Sandbox Code Playgroud) android android-layout android-actionbar android-5.0-lollipop android-toolbar