我一直在尝试重现滚动列表视图时版本5.0上的“联系人”应用折叠工具栏的方式。
屏幕快照库,展示了所需的交互作用请注意,工具栏会分阶段折叠,其中显示搜索+最后一个联系人,淡入最后一个联系人,折叠最后一个联系人,折叠搜索,仅保留选项卡。
到目前为止,我在LinearLayout的recyclerview上方有一个工具栏,该工具栏用作操作栏,而不是独立的。
我无法弄清楚如何在recyclerview上拦截touch事件并使它收缩工具栏,然后将滚动事件返回给recyclerview。我试图将整个东西放到一个滚动视图中,但是随后recyclerview无法正确计算它的高度,并且不显示任何内容。我尝试在recyclerview上覆盖onscroll,发现它只会在滚动事件开始时通知我,并向我提供第一个可见的卡ID。
看起来不错但我终生无法工作的方式是:
getSupportActionBar().setHideOnContentScrollEnabled(true);
哪个返回:
Caused by: java.lang.UnsupportedOperationException: Hide on content scroll is not supported in this action bar configuration.
使用传统的动作栏,在其下方放置工具栏并设置hideoncontentscrollenabled也不起作用,滚动从未触发动作栏上的hide方法。
-编辑-我能够使用传统的操作栏使hideOnContentScrollEnabled在列表视图上工作,但是其行为与通讯录应用不同。显然这不是他们使用的方法-当在列表视图上发生fling事件时,它只是在操作栏上触发.hide(),这与Contacts应用程序不同,后者与滚动事件一起拖动了工具栏。 - /编辑 -
因此,我放弃了该路线,并在cardview高度上设置fill_parent,并在工具栏上设置了一个折叠动画。但是,如何触发它,使其在触摸事件之后,然后将触摸事件返回给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>
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>
styles.xml
...
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
MainActivity.java
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
// Disable the logo in the actionbar, as per material guidelines
toolbar.getMenu().clear();
toolbar.setTitle("My toolbar");
setSupportActionBar(toolbar);
我还没有研究过源代码,但是这个家伙似乎使生活变得轻松而富有启发性。
https://github.com/ksoichiro/Android-ObservableScrollView
编辑
谷歌刚刚发布了Android设计库。请看一下它,它包含折叠工具栏的所有效果以及更多其他功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句