我尝试在Google Maps Android应用中实现搜索栏:
当“回收者”视图处于初始状态时,工具栏没有高程。仅当用户开始滚动时,高程才可见。搜索栏(工具栏)永不崩溃。这是我试图复制的内容:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="64dp">
<!-- content -->
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
在这里您可以看到结果:
所以我的解决方案的问题是,工具栏的高度始终可见。但我希望它仅在回收者视图在其后滚动时显示。设计支持库中是否有任何东西可以实现google maps应用程序中所示的行为?
我在用
com.android.support:appcompat-v7:23.2.0
com.android.support:design:23.2.0
编辑正如评论中指出的那样,我的答案现在已经过时,请参阅https://stackoverflow.com/a/58272283/4291272
无论您是否使用a CoordinatorLayout
,RecyclerView.OnScrollListener
就海拔而言,a似乎都是正确的选择。但是,根据我的经验,这recyclerview.getChild(0).getTop()
是不可靠的,因此不应用于确定滚动状态。相反,这是起作用的:
private static final int SCROLL_DIRECTION_UP = -1;
// ...
// Put this into your RecyclerView.OnScrollListener > onScrolled() method
if (recyclerview.canScrollVertically(SCROLL_DIRECTION_UP)) {
// Remove elevation
toolbar.setElevation(0f);
} else {
// Show elevation
toolbar.setElevation(50f);
}
请务必将分配LayoutManager
给您RecyclerView
或canScrollVertically的调用可能会导致崩溃!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句