让你玩转滑动列表上滑隐藏和下滑显示控件2

本文介绍了如何使用CoordinatorLayout和Behavior特性,实现滑动列表时FloatingActionButton和Toolbar的隐藏与显示效果。通过CoordinatorLayout的layout_behavior属性,结合自定义Behavior类,监听RecyclerView的滑动,实现了更优雅的交互体验。同时提到了AppBarLayout在手势滚动中的作用。
摘要由CSDN通过智能技术生成

1.CoordinatorLayout的出现。
继上一篇https://blog.csdn.net/m0_37667770/article/details/79606434博客,对于这些个性化的设计我想民间这么通用,谷歌工程师也会照猫画虎去设计来提供开发者提供更好的方法把。果不其然,CoordinatorLayout就有很好的解决了这个也算是麻烦的设计。CoordinatorLayout如果你查看了google提供的api那么就会知道CoordinatorLayout的重要性了。自己去看看谷歌介绍,如下:http://android.xsoftlab.net/reference/android/support/design/widget/CoordinatorLayout.html

As a container for a specific interaction with one or more child views
By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout using the DefaultBehavior annotation.
Behaviors may be used to implement a variety of interactions and additional layout modifications ranging from sliding drawers and panels to swipe-dismissable elements and buttons that stick to other elements as they move and animate.
Children of a CoordinatorLayout may have an anchor. This view id must correspond to an arbitrary descendant of the CoordinatorLayout, but it may not be the anchored child itself or a descendant of the anchored child. This can be used to place floating views relative to other arbitrary content panes.

作为一个协调一个或多个子控件的容器控件。它会带给你跟多的帮助。

2.属性Behavior:
上面也说了可以通过CoordinatorLayout的属性Behavior来和其他子控件进行各种交互。可以去API中仔细看看Behavior的作用:可以和子控件进行交互,滑动,拖拽,闪动等。

A Behavior implements one or more interactions that a user can take on a child view. These interactions may include drags, swipes, flings, or any other gestures.

3.看看效果如下:
这里写图片描述
这里写图片描述

4.上代码来实现我们FloatingActionButton和Toobar的隐藏和显示
上一篇总我们通过滑动RecylclerView监听滑动的dy来通过接口设置属性动画解决的可以让Toobar和下面的FloatingActionButton上滑消失下滑显示。今天我们通过CoordinatorLayout来进行解决达到这个效果。
第一CoordinatorLayout作为协调容器控件,对于自己所包含的控件可以协调进行子控件之间的交互,这里监听滑动控件RecyclerView的滑动通过Behavior反馈到FloatingActionButton。它为我们提供了继承类 CoordinatorLayout.Behavior而且他有子类的…自己可以查看哦!这里不多说了。它提供了layout_behavior属性进行关联子空间和滑动控件进行交互。代码如下:
1.在布局文件中

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ls.lsn16_materialdesign_fab_animation_behavior.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:paddingTop="42dp"
        android:clipToPadding="false"
        android:clipChildren="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <android.support.v7.widget.Toolbar

        
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值